From ba2e6d3b68101ccdd6cbda865d5cf3860890c1c9 Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Fri, 9 Sep 2022 00:46:40 -0400 Subject: [PATCH] V0.4.0 - Added onlyOneReaction system +deno fmt --- README.md | 2 +- config.example.ts | 2 +- deps.ts | 2 +- src/events/messageCreate.ts | 11 --------- src/events/reactionAdd.ts | 6 ++--- src/functions/cleanRaidCheckpointChannel.ts | 10 ++++---- src/functions/onlyOneReaction.ts | 27 +++++++++++++++++++-- src/functions/pollReactions.ts | 2 +- 8 files changed, 36 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index f6572e8..18d4e94 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Sweeper Bot | V0.2.0 - 2022/09/03 +# Sweeper Bot | V0.4.0 - 2022/09/09 [![SonarCloud](https://sonarcloud.io/images/project_badges/sonarcloud-orange.svg)](https://sonarcloud.io/summary/new_code?id=SweeperBot) [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=SweeperBot&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=SweeperBot) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=SweeperBot&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=SweeperBot) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=SweeperBot&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=SweeperBot) [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=SweeperBot&metric=bugs)](https://sonarcloud.io/summary/new_code?id=SweeperBot) [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=SweeperBot&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=SweeperBot) [![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=SweeperBot&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=SweeperBot) diff --git a/config.example.ts b/config.example.ts index 0e15055..e71aa1a 100644 --- a/config.example.ts +++ b/config.example.ts @@ -1,6 +1,6 @@ export const config = { 'name': 'Sweeper Bot', // Name of the bot - 'version': '0.3.0', // Version of the bot + 'version': '0.4.0', // Version of the bot 'token': 'the_bot_token', // Discord API Token for this bot 'localtoken': 'local_testing_token', // Discord API Token for a secondary OPTIONAL testing bot, THIS MUST BE DIFFERENT FROM "token" 'prefix': 's!', // Prefix for all commands diff --git a/deps.ts b/deps.ts index 72925ce..bec5ddb 100644 --- a/deps.ts +++ b/deps.ts @@ -4,7 +4,7 @@ import config from './config.ts'; import { LOCALMODE } from './flags.ts'; export const botId = getBotIdFromToken(LOCALMODE ? config.localtoken : config.token); -export { ActivityTypes, createBot, editBotMember, editBotStatus, Intents, sendMessage, startBot, getReactions } from 'https://deno.land/x/discordeno@14.0.1/mod.ts'; +export { ActivityTypes, createBot, editBotMember, editBotStatus, getReactions, Intents, sendMessage, startBot } from 'https://deno.land/x/discordeno@14.0.1/mod.ts'; export type { Bot, CreateMessage, Emoji, EventHandlers, Guild, Member, Message, User } from 'https://deno.land/x/discordeno@14.0.1/mod.ts'; diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 172099c..98bb993 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -82,17 +82,6 @@ export const messageCreate = async (bot: Bot, message: Message) => { commands.sendMessage(bot, message, args); } break; - // case 'emoji-test': - // const test = await bot.helpers.getMessage(413640605491658754n, 1016090989816987701n); - // console.log(test) - // if (test.reactions && test.reactions.length) { - // console.log(test.reactions[0]) - // const what = `${test.reactions[0].emoji.name}${test.reactions[0].emoji.id ? `:${test.reactions[0].emoji.id}` : ''}`; - // console.log(what) - // await getReactions(bot, 413640605491658754n, 1016090989816987701n, what) - // } - // // bot.helpers - // break; default: // Non-standard commands console.log(`${command} WIP`); diff --git a/src/events/reactionAdd.ts b/src/events/reactionAdd.ts index 739ece6..f114541 100644 --- a/src/events/reactionAdd.ts +++ b/src/events/reactionAdd.ts @@ -11,11 +11,9 @@ export const reactionAdd = async (bot: Bot, payload: ReactionAdd) => { if (config.pollChannels.includes(payload.channelId)) { try { const message = await bot.helpers.getMessage(payload.channelId, payload.messageId); - const onlyOneWordRX = /(only one)/g + const onlyOneWordRX = /(only one)/g; if (message.content.toLowerCase().includes('clan poll') && message.content.toLowerCase().match(onlyOneWordRX)?.length) { - // functions.onlyOneReaction(bot, message); - - // bot.helpers.getReactions() + functions.onlyOneReaction(bot, payload, message); } } catch (e) { utils.commonLoggers.messageGetError('reactionAdd.ts:14', `failed to get message ${payload.channelId}-${payload.messageId}`, e); diff --git a/src/functions/cleanRaidCheckpointChannel.ts b/src/functions/cleanRaidCheckpointChannel.ts index 2826578..6543657 100644 --- a/src/functions/cleanRaidCheckpointChannel.ts +++ b/src/functions/cleanRaidCheckpointChannel.ts @@ -1,19 +1,19 @@ -import { Bot } from "../../deps.ts"; -import utils from "../utils.ts"; +import { Bot } from '../../deps.ts'; +import utils from '../utils.ts'; export const cleanRaidCheckpointChannel = async (bot: Bot, channelId: bigint) => { try { // Get messages in channel, sort them oldest=>newest, and filter to messages from followed servers - const messages = (await bot.helpers.getMessages(channelId)).array().sort((a, b) => a.timestamp - b.timestamp).filter(msg => msg.isFromBot && msg.messageReference); + const messages = (await bot.helpers.getMessages(channelId)).array().sort((a, b) => a.timestamp - b.timestamp).filter((msg) => msg.isFromBot && msg.messageReference); // Remove most recent message from array messages.pop(); // Delete all other messages for (const message of messages) { - bot.helpers.deleteMessage(message.channelId, message.id, 'Old Checkpoint Message').catch(e => utils.commonLoggers.messageDeleteError('cleanRaidCheckpointChannel.ts:14', message, e)); + bot.helpers.deleteMessage(message.channelId, message.id, 'Old Checkpoint Message').catch((e) => utils.commonLoggers.messageDeleteError('cleanRaidCheckpointChannel.ts:14', message, e)); } } catch (e) { - utils.commonLoggers.messageGetError('cleanRaidCheckpointChannel.ts:17', 'Something broke', e) + utils.commonLoggers.messageGetError('cleanRaidCheckpointChannel.ts:17', 'Something broke', e); } }; diff --git a/src/functions/onlyOneReaction.ts b/src/functions/onlyOneReaction.ts index f32ac8e..e0af405 100644 --- a/src/functions/onlyOneReaction.ts +++ b/src/functions/onlyOneReaction.ts @@ -1,6 +1,29 @@ import { // Discordeno deps Bot, -} from "../../deps.ts"; + Emoji, + Message, +} from '../../deps.ts'; +import { ReactionAdd } from '../types/eventTypes.ts'; +import utils from '../utils.ts'; -export const onlyOneReaction = (bot: Bot) => { }; +const emojiName = (emoji: Emoji) => { + const emojiId = emoji.id ? `:${emoji.id}` : ''; + return `${emoji.name}${emojiId}`; +}; + +export const onlyOneReaction = async (bot: Bot, payload: ReactionAdd, message: Message) => { + const newEmoji = emojiName(payload.emoji); + + if (message.reactions) { + for (const reaction of message.reactions) { + const otherEmoji = emojiName(reaction.emoji); + + if (newEmoji !== otherEmoji) { + bot.helpers.deleteUserReaction(message.channelId, message.id, payload.userId, otherEmoji).catch((e) => + utils.commonLoggers.reactionDeleteError('onlyOneReaction.ts:23', message, e, otherEmoji) + ); + } + } + } +}; diff --git a/src/functions/pollReactions.ts b/src/functions/pollReactions.ts index 8d2ca12..65bc66a 100644 --- a/src/functions/pollReactions.ts +++ b/src/functions/pollReactions.ts @@ -40,7 +40,7 @@ export const pollReactions = async (bot: Bot, message: Message, update = false) await bot.helpers.addReaction(message.channelId, message.id, emoji).catch(async (_err) => { try { const [animated, emojiName, emojiId] = emoji.split(':'); - const newEmoji = await bot.helpers.createEmoji(config.devServer, {name: emojiName, image: `https://cdn.discordapp.com/emojis/${emojiId}.${animated ? 'gif' : 'webp'}`}) + const newEmoji = await bot.helpers.createEmoji(config.devServer, { name: emojiName, image: `https://cdn.discordapp.com/emojis/${emojiId}.${animated ? 'gif' : 'webp'}` }); await bot.helpers.addReaction(message.channelId, message.id, `:${newEmoji.name}:${newEmoji.id}`); await bot.helpers.deleteEmoji(config.devServer, newEmoji.id || 0n); } catch (e) {