This repository has been archived on 2023-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
nimcord/examples/basic.nim

123 lines
5.0 KiB
Nim
Raw Normal View History

2020-06-23 05:08:25 +00:00
import ../src/nimcord, asyncdispatch, streams, os, strutils, options, websocket
2020-06-22 21:57:54 +00:00
var tokenStream = newFileStream("token.txt", fmRead)
var tkn: string
if (not isNil(tokenStream)):
discard tokenStream.readLine(tkn)
tokenStream.close()
var bot = newDiscordClient(tkn, "?", newLog(ord(LoggerFlags.loggerFlagDebugSeverity)))
2020-06-22 21:57:54 +00:00
2020-08-13 23:05:23 +00:00
let pingCommand = Command(name: "ping", commandBody: proc(ctx: CommandContext) =
2020-08-28 21:02:38 +00:00
asyncCheck ctx.channel.sendMessage("PONG")
2020-08-13 23:05:23 +00:00
)
let modifyChannelTopicCommand = Command(name: "modifyChannelTopic", commandBody: proc(ctx: CommandContext) =
let modifyTopic = ctx.message.content.substr(20)
2020-08-28 21:02:38 +00:00
asyncCheck ctx.channel.sendMessage("Modifing Channel!")
asyncCheck ctx.channel.modifyChannel(ChannelFields(topic: some(modifyTopic)))
2020-08-13 23:05:23 +00:00
)
let deleteChannelCommand = Command(name: "deleteChannel", commandBody: proc(ctx: CommandContext) =
let channelID = getIDFromJson(ctx.arguments[0])
var channel: Channel = ctx.client.cache.getChannel(channelID)
# Check if we could find the channel to delete
if (channel != nil):
2020-08-28 21:02:38 +00:00
asyncCheck channel.sendMessage("Deleting Channel!")
asyncCheck channel.deleteChannel()
asyncCheck ctx.channel.sendMessage("Deleted Channel!")
2020-08-13 23:05:23 +00:00
)
let bulkDeleteMessagesCommand = Command(name: "bulkDeleteMessages", commandBody: proc(ctx: CommandContext) =
var amount: int = 25
if (ctx.message.content.len > 19):
amount = parseIntEasy(ctx.arguments[0])
# Get the message to delete, then delete them.
let messages = ctx.channel.getMessages(MessagesGetRequest(limit: some(amount), before: some(ctx.message.id)))
2020-08-28 21:02:38 +00:00
asyncCheck ctx.channel.bulkDeleteMessages(messages)
2020-08-13 23:05:23 +00:00
# Delete the message that was used to run this command.
2020-08-28 21:02:38 +00:00
asyncCheck ctx.message.deleteMessage()
2020-08-13 23:05:23 +00:00
)
let reactToMessageCommand = Command(name: "reactToMessage", commandBody: proc(ctx: CommandContext) =
let emojis = @[newEmoji("⏮️"), newEmoji("⬅️"), newEmoji("⏹️"), newEmoji("➡️"), newEmoji("⏭️")]
for emoji in emojis:
2020-08-28 21:02:38 +00:00
asyncCheck ctx.message.addReaction(emoji)
2020-08-13 23:05:23 +00:00
)
let testEmbedCommand = Command(name: "testEmbed", commandBody: proc(ctx: CommandContext) =
var embed = Embed()
embed.setTitle("This embed is being sent from Nimcord!")
embed.setDescription("Nimcord was developed in about a week of actual work so there will likely be issues.")
embed.addField("Title", "value")
embed.addField("Inline-0", "This is an inline field 0", true)
embed.addField("Inline-1", "This is an inline field 1", true)
embed.setColor(0xffb900)
2020-08-28 21:02:38 +00:00
asyncCheck ctx.channel.sendMessage("", false, embed)
2020-08-13 23:05:23 +00:00
)
let sendFileCommand = Command(name: "sendFile", commandBody: proc(ctx: CommandContext) =
let filePath = ctx.message.content.substr(10)
let splitFile = splitFile(filePath)
let fileName = splitFile.name & splitFile.ext
let file = DiscordFile(filePath: filePath, fileName: fileName)
2020-08-28 21:02:38 +00:00
asyncCheck ctx.channel.sendMessage("", false, nil, @[file])
2020-08-13 23:05:23 +00:00
)
let sendImageCommand = Command(name: "sendImage", commandBody: proc(ctx: CommandContext) =
let filePath = ctx.message.content.substr(11)
let splitFile = splitFile(filePath)
let fileName = splitFile.name & splitFile.ext
let file = DiscordFile(filePath: filePath, fileName: fileName)
var embed = Embed()
embed.setTitle("Image attachment test.")
embed.setImage("attachment://" & fileName)
2020-08-28 21:02:38 +00:00
asyncCheck ctx.channel.sendMessage("", false, embed, @[file])
2020-08-13 23:05:23 +00:00
)
# You can even register commands like this:
registerCommand(Command(name: "ping2", commandBody: proc(ctx: CommandContext) =
2020-08-28 21:02:38 +00:00
asyncCheck ctx.channel.sendMessage("PONG 2")
))
registerCommand(Command(name: "reconnect", commandBody: proc(ctx: CommandContext) =
asyncCheck ctx.channel.sendMessage("Reconnecting...")
asyncCheck ctx.client.shards[0].reconnectShard()
2020-08-13 23:05:23 +00:00
))
# Listen for the ready event.
2020-06-22 21:57:54 +00:00
registerEventListener(EventType.evtReady, proc(bEvt: BaseEvent) =
2020-08-13 23:05:23 +00:00
# Cast the BaseEvent to the ReadyEvent which is what we're listening to.
let event = ReadyEvent(bEvt)
2020-06-22 21:57:54 +00:00
event.shard.client.log.info("Ready!")
event.shard.client.log.info("Logged in as: " & bot.clientUser.username & "#" & $bot.clientUser.discriminator)
event.shard.client.log.info("ID: " & $bot.clientUser.id)
event.shard.client.log.info("--------------------")
2020-06-22 21:57:54 +00:00
let presence = newPresence("with Nimcord", ActivityType.activityTypeGame,
ClientStatus.clientStatusIdle, false)
2020-06-23 05:08:25 +00:00
asyncCheck event.shard.updateClientPresence(presence)
2020-06-22 21:57:54 +00:00
2020-08-13 23:05:23 +00:00
# Register commands. You don't need to register them in EventReady.
registerCommand(pingCommand)
registerCommand(modifyChannelTopicCommand)
registerCommand(deleteChannelCommand)
registerCommand(bulkDeleteMessagesCommand)
registerCommand(reactToMessageCommand)
registerCommand(testEmbedCommand)
registerCommand(sendFileCommand)
registerCommand(sendImageCommand)
2020-06-22 21:57:54 +00:00
)
waitFor bot.startConnection()