From 22c703b703c7fa7596f6f9aa3165063142aae674 Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Thu, 6 May 2021 15:57:32 -0700 Subject: [PATCH] Moved to yaml for easier command adding --- .gitignore | 2 +- boardsourcebot.nimble | 1 + commands.yaml | 51 ++++++++++++ src/boardsourcebot.nim | 83 +++++++------------ ...{apikey.nim.example => config.nim.example} | 0 5 files changed, 82 insertions(+), 55 deletions(-) create mode 100644 commands.yaml rename src/{apikey.nim.example => config.nim.example} (100%) diff --git a/.gitignore b/.gitignore index e1b7a3a..3bbd10b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ tmp/ -src/apikey.nim +src/config.nim boardsourcebot diff --git a/boardsourcebot.nimble b/boardsourcebot.nimble index 6d23834..1e8ba47 100644 --- a/boardsourcebot.nimble +++ b/boardsourcebot.nimble @@ -12,3 +12,4 @@ bin = @["boardsourcebot"] requires "nim >= 1.4.6" requires "dimscord >= 1.2.5" +requires "https://github.com/flyx/NimYAML#devel" diff --git a/commands.yaml b/commands.yaml new file mode 100644 index 0000000..e13c33a --- /dev/null +++ b/commands.yaml @@ -0,0 +1,51 @@ +%YAML 1.2 +--- +- { + command: "brokenkey", + response: "Broken key tips can be found here", + response2: "https://discord.com/channels/704394060390989834/705657159639105626/798051589004197938" + } +- { + command: "buildguide", + response: "Build Guides can be found here", + response2: "https://discord.com/channels/704394060390989834/705657159639105626/790728181857910854" + } +- { + command: "trrs", + response: "TRRS jacks are included with the PCB, as are the reset buttons", + response2: "" + } +- { + command: "restock", + response: "https://media1.tenor.com/images/28cf6532c043be996d04c00ec2ac3246/tenor.gif?itemid=2088319", + response2: "Shipping is backup up worldwide and we can not give estimates at the current time" + } +- { + command: "oled", + response: "OLED help can be found here", + response2: "https://discord.com/channels/704394060390989834/755980550539313293/790417018822197289" + } +- { + command: "sockets", + response: "Socket information and links can be found here", + response2: "https://discord.com/channels/704394060390989834/705657159639105626/752342450047156245" + } +- { + command: "configure", + response: + "Hitting configure will add at least one of each item you NEED For a +working keyboard, not including switches or keycaps. For example, it will +include 1 PCB or pair of PCBs for splits. You may want to add or change items to +customize it for your needs or desires though! +", + response2: "" + } +- { + command: "elitec", + response: + "The elite C uses a type-C port as opposed to microUSB making it less prone + to breaking, as well as having a better bootloader, which is especially + important for linux users. +", + response2: "" + } diff --git a/src/boardsourcebot.nim b/src/boardsourcebot.nim index c1484dd..64eb11a 100644 --- a/src/boardsourcebot.nim +++ b/src/boardsourcebot.nim @@ -1,5 +1,28 @@ -include apikey +include config import dimscord, asyncdispatch, options, strformat, strutils +import yaml/serialization, streams + +type CommandList = object + command : string + response : string + response2 : string + +# YAML parser +proc registerCommands(file: string):seq[CommandList] = + var commandList: seq[CommandList] + var s = newFileStream(file) + load(s, commandList) + s.close() + return commandList + +let commands = registerCommands("commands.yaml") + +func checkForCmd(prefix:string, commands:seq[CommandList], m:Message): seq[string] = + for i in commands: + if fmt"{prefix}{i.command}" == m.content: + return @[i.response, i.response2] + return @[""] + let discord = newDiscordClient(apikey) @@ -9,59 +32,11 @@ proc onReady(s: Shard, r: Ready) {.event(discord).} = # Handle event for message_create. proc messageCreate(s: Shard, m: Message) {.event(discord).} = + let responseSeq = checkForCmd(prefix, commands, m) if m.author.bot: return - - if m.content == fmt"{prefix}help": - let msg = await discord.api.sendMessage(m.channel_id, - fmt""" - Commands are prefixed with {prefix}. Avaliable commands are help, brokenkey, - buildguide, trrs, restock, shipping, oled, configure, elitec, sockets - """.dedent.splitlines.join(" ")) - - elif m.content == fmt"{prefix}brokenkey": - let msg = await discord.api.sendMessage(m.channel_id, "Broken key tips can be found here") - let link = await discord.api.sendMessage(m.channel_id, - "https://discord.com/channels/704394060390989834/705657159639105626/798051589004197938") - - elif m.content == fmt"{prefix}buildguide": - let msg = await discord.api.sendMessage(m.channel_id, "Build guides can be found here") - let link = await discord.api.sendMessage(m.channel_id, - "https://discord.com/channels/704394060390989834/705657159639105626/790728181857910854") - - elif m.content == fmt"{prefix}trrs": - let msg = await discord.api.sendMessage(m.channel_id, "TRRS jacks are included with the PCB, as are the reset buttons") - - elif m.content == fmt"{prefix}restock" or m.content == - fmt"{prefix}shipping": - let gif = await discord.api.sendMessage(m.channel_id, - "https://media1.tenor.com/images/28cf6532c043be996d04c00ec2ac3246/tenor.gif?itemid=2088319") - let msg = await discord.api.sendMessage(m.channel_id, - "Shipping is backup up worldwide and we can not give estimates at the current time") - - elif m.content == fmt"{prefix}oled": - let msg = await discord.api.sendMessage(m.channel_id, "OLED help can be found here") - let link = await discord.api.sendMessage(m.channel_id, - "https://discord.com/channels/704394060390989834/755980550539313293/790417018822197289") - - elif m.content == fmt"{prefix}configure": - let msg = await discord.api.sendMessage(m.channel_id, - """ - Hitting configure will add at least one of each item you NEED For a working - keyboard, not including switches or keycaps. For example, it will include 1 - PCB or pair of PCBs for splits. You may want to add or change items to - customize it for your needs or desires though! - """.dedent.splitlines.join(" ")) - - elif m.content == fmt"{prefix}elitec": - let msg = await discord.api.sendMessage(m.channel_id, - """ - The elite C uses a type-C port as opposed to microUSB making it less prone - to breaking, as well as having a better bootloader, which is especially - important for linux users. - """.dedent.splitlines.join(" ")) - - elif m.content == fmt"{prefix}sockets": - let msg = await discord.api.sendMessage(m.channel_id, "Socket information and links can be found here") - let link = await discord.api.sendMessage(m.channel_id, "https://discord.com/channels/704394060390989834/705657159639105626/752342450047156245") + if not responseSeq[0].isEmptyOrWhitespace: # Validates response to command exists + let msg = await discord.api.sendMessage(m.channel_id, responseSeq[0]) + if not responseSeq[1].isEmptyOrWhitespace: + let msg2 = await discord.api.sendMessage(m.channel_id, responseSeq[1]) waitFor discord.startSession() diff --git a/src/apikey.nim.example b/src/config.nim.example similarity index 100% rename from src/apikey.nim.example rename to src/config.nim.example