cleanup
This commit is contained in:
@@ -23,8 +23,16 @@ struct Nahi {
|
|||||||
|
|
||||||
let bot = await BotGatewayManager(
|
let bot = await BotGatewayManager(
|
||||||
token: discordToken,
|
token: discordToken,
|
||||||
presence: .init(activities: [.init(name: "Vibing", type: .competing)], status: .online, afk: false),
|
presence: .init(
|
||||||
intents: [.guildMessages, .messageContent, .directMessages]
|
activities: [.init(name: "Vibing", type: .competing)],
|
||||||
|
status: .online,
|
||||||
|
afk: false
|
||||||
|
),
|
||||||
|
intents: [
|
||||||
|
.guildMessages,
|
||||||
|
.messageContent,
|
||||||
|
//.directMessages
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
let gemini = GeminiKit(
|
let gemini = GeminiKit(
|
||||||
@@ -59,7 +67,13 @@ struct Nahi {
|
|||||||
|
|
||||||
taskGroup.addTask {
|
taskGroup.addTask {
|
||||||
for await event in await bot.events {
|
for await event in await bot.events {
|
||||||
await EventHandler(event: event, client: bot.client, gemini: gemini, log: log, aiChat: aiChat).handleAsync()
|
await EventHandler(
|
||||||
|
event: event,
|
||||||
|
client: bot.client,
|
||||||
|
gemini: gemini,
|
||||||
|
log: log,
|
||||||
|
aiChat: aiChat
|
||||||
|
).handleAsync()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,17 +88,21 @@ struct EventHandler: GatewayEventHandler {
|
|||||||
let aiChat: Chat
|
let aiChat: Chat
|
||||||
|
|
||||||
func onMessageCreate(_ payload: Gateway.MessageCreate) async throws {
|
func onMessageCreate(_ payload: Gateway.MessageCreate) async throws {
|
||||||
let channel = try? await client.getChannel(id: payload.channel_id)
|
|
||||||
guard
|
guard
|
||||||
payload.author!.id.rawValue != client.appId!.rawValue,
|
payload.author!.id.rawValue != client.appId!.rawValue,
|
||||||
payload.mentions.contains(where: { mention in mention.id.rawValue == client.appId!.rawValue }) || (try? channel?.decode().type ?? .guildText) == .dm
|
payload.mentions.contains(where: { mention in
|
||||||
|
mention.id.rawValue == client.appId!.rawValue
|
||||||
|
})
|
||||||
else { return }
|
else { return }
|
||||||
|
|
||||||
_ = try await client.triggerTypingIndicator(channelId: payload.channel_id)
|
_ = try await client.triggerTypingIndicator(channelId: payload.channel_id)
|
||||||
|
|
||||||
let prompt = "\(payload.author!.global_name!): \(payload.content.replacingOccurrences(of: "<@\(client.appId!.rawValue)>", with: "@Nahida"))"
|
let author = payload.author?.global_name ?? payload.author?.username ?? "Unknown"
|
||||||
|
let message = payload.content
|
||||||
|
.replacingOccurrences(of: "<@\(client.appId!.rawValue)>", with: "@Nahida")
|
||||||
|
let prompt = "\(author): \(message)"
|
||||||
|
|
||||||
let aiRes = await Task {
|
let aiRes = await Task {
|
||||||
//return try await gemini.generateContent(model: .gemini20Flash, prompt: prompt, systemInstruction: "You are a discord chat bot pretending to be Nahida, the dendro archon from the game Genshin Impact. Act like Nahida in your responces.")
|
|
||||||
return try await aiChat.sendMessage(prompt)
|
return try await aiChat.sendMessage(prompt)
|
||||||
}.result
|
}.result
|
||||||
|
|
||||||
@@ -98,19 +116,34 @@ struct EventHandler: GatewayEventHandler {
|
|||||||
for text in resStr {
|
for text in resStr {
|
||||||
let messageRes = try await client.createMessage(
|
let messageRes = try await client.createMessage(
|
||||||
channelId: payload.channel_id,
|
channelId: payload.channel_id,
|
||||||
payload: .init(content: text, message_reference: .init(message_id: payload.id, channel_id: payload.channel_id, guild_id: payload.guild_id, fail_if_not_exists: false))
|
payload: .init(
|
||||||
|
content: text,
|
||||||
|
message_reference: .init(
|
||||||
|
message_id: payload.id,
|
||||||
|
channel_id: payload.channel_id,
|
||||||
|
guild_id: payload.guild_id,
|
||||||
|
fail_if_not_exists: false
|
||||||
|
)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
try messageRes.guardSuccess()
|
try messageRes.guardSuccess()
|
||||||
}
|
}
|
||||||
case .failure(let err):
|
case .failure(let err):
|
||||||
|
let errMessage = "Someone tell <@259709415416922113> there is a problem with my AI: \(String(describing: err))"
|
||||||
try await client.createMessage(
|
try await client.createMessage(
|
||||||
channelId: payload.channel_id,
|
channelId: payload.channel_id,
|
||||||
payload: .init(content: "Someone tell <@259709415416922113> there is a problem with my AI: \(String(describing: err))", message_reference: .init(message_id: payload.id, channel_id: payload.channel_id, guild_id: payload.guild_id, fail_if_not_exists: false))
|
payload: .init(
|
||||||
|
content: errMessage,
|
||||||
|
message_reference: .init(
|
||||||
|
message_id: payload.id,
|
||||||
|
channel_id: payload.channel_id,
|
||||||
|
guild_id: payload.guild_id,
|
||||||
|
fail_if_not_exists: false
|
||||||
|
)
|
||||||
|
)
|
||||||
).guardSuccess()
|
).guardSuccess()
|
||||||
log.error("\(err)")
|
log.error("\(err)")
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user