From 083481667d6de247e350aed55d0cc8a55f7b1158 Mon Sep 17 00:00:00 2001 From: Brian Date: Fri, 1 Oct 2021 15:22:34 -0400 Subject: [PATCH 1/2] enhance stability --- CHANGELOG.md | 4 +++ src/app/events/GuildBanRemove.ts | 22 +++++++----- src/app/events/GuildMemberAdd.ts | 8 +++-- src/app/events/GuildMemberUpdate.ts | 36 ++++++++++---------- src/app/events/MessageCreate.ts | 21 ++++++++---- src/app/events/MessageReactionAdd.ts | 37 ++++++++++----------- src/app/events/Ready.ts | 24 +++++++------ src/app/events/UserUpdate.ts | 15 ++++----- src/app/events/VoiceStateUpdate.ts | 1 - src/app/events/chat/MessageCreateOnDEGEN.ts | 11 ++++++ 10 files changed, 105 insertions(+), 74 deletions(-) create mode 100644 src/app/events/chat/MessageCreateOnDEGEN.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a90d7821..6e05fe56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.6.2-RELEASE (2021-10-01) + +1. Enhance stability of DEGEN + ## 1.6.1-RELEASE (2021-10-01) 1. Reference production environment files diff --git a/src/app/events/GuildBanRemove.ts b/src/app/events/GuildBanRemove.ts index 7482dccd..87367551 100644 --- a/src/app/events/GuildBanRemove.ts +++ b/src/app/events/GuildBanRemove.ts @@ -10,16 +10,20 @@ export default class implements DiscordEvent { once = false; async execute(ban: GuildBan): Promise { - // Add unbanned users to allowlist so they don't get auto-banned by the bot - const db: Db = await dbInstance.dbConnect(constants.DB_NAME_DEGEN); - const dbAllowlist = db.collection(constants.DB_COLLECTION_ALLOWLIST); + try { + // Add unbanned users to allowlist so they don't get auto-banned by the bot + const db: Db = await dbInstance.dbConnect(constants.DB_NAME_DEGEN); + const dbAllowlist = db.collection(constants.DB_COLLECTION_ALLOWLIST); - const result: InsertOneWriteOpResult = await dbAllowlist.insertOne({ - discordUserId: ban.user.id, - discordServerId: ban.guild.id, - }); - if (result == null || result.insertedCount !== 1) { - throw new MongoError(`failed to insert ${ban.user.id} into allowlist`); + const result: InsertOneWriteOpResult = await dbAllowlist.insertOne({ + discordUserId: ban.user.id, + discordServerId: ban.guild.id, + }); + if (result == null || result.insertedCount !== 1) { + throw new MongoError(`failed to insert ${ban.user.id} into allowlist`); + } + } catch (e) { + console.error(e); } } } \ No newline at end of file diff --git a/src/app/events/GuildMemberAdd.ts b/src/app/events/GuildMemberAdd.ts index 046120ce..85fc25d4 100644 --- a/src/app/events/GuildMemberAdd.ts +++ b/src/app/events/GuildMemberAdd.ts @@ -7,8 +7,12 @@ export default class implements DiscordEvent { once = false; async execute(member: GuildMember): Promise { - if (await ServiceUtils.runUsernameSpamFilter(member)) { - return; + try { + if (await ServiceUtils.runUsernameSpamFilter(member)) { + return; + } + } catch (e) { + console.error(e); } } } \ No newline at end of file diff --git a/src/app/events/GuildMemberUpdate.ts b/src/app/events/GuildMemberUpdate.ts index 536274f2..5be021ae 100644 --- a/src/app/events/GuildMemberUpdate.ts +++ b/src/app/events/GuildMemberUpdate.ts @@ -18,27 +18,29 @@ export default class implements DiscordEvent { if (newMember.partial) { newMember = await newMember.fetch(); } + + if (oldMember.nickname !== newMember.nickname && await ServiceUtils.runUsernameSpamFilter(newMember as GuildMember)) { + return; + } + + const removedRoles = oldMember.roles.cache.filter(role => !newMember.roles.cache.has(role.id)); + if (removedRoles.size > 0) { + console.debug(`The roles ${removedRoles.map(r => r.name)} were removed from ${oldMember.displayName}.`); + this.handleRolesRemoved(newMember as GuildMember, removedRoles); + return; + } + + const addedRoles = newMember.roles.cache.filter(role => !oldMember.roles.cache.has(role.id)); + if (addedRoles.size > 0) { + console.debug(`The roles ${addedRoles.map(r => r.name)} were added to ${oldMember.displayName}.`); + this.handleRolesAdded(newMember as GuildMember, addedRoles); + } + } catch (e) { + console.error(e); console.error('Retrieving member partial failed'); return; } - - if (oldMember.nickname !== newMember.nickname && await ServiceUtils.runUsernameSpamFilter(newMember as GuildMember)) { - return; - } - - const removedRoles = oldMember.roles.cache.filter(role => !newMember.roles.cache.has(role.id)); - if (removedRoles.size > 0) { - console.debug(`The roles ${removedRoles.map(r => r.name)} were removed from ${oldMember.displayName}.`); - this.handleRolesRemoved(newMember as GuildMember, removedRoles); - return; - } - - const addedRoles = newMember.roles.cache.filter(role => !oldMember.roles.cache.has(role.id)); - if (addedRoles.size > 0) { - console.debug(`The roles ${addedRoles.map(r => r.name)} were added to ${oldMember.displayName}.`); - this.handleRolesAdded(newMember as GuildMember, addedRoles); - } } /** diff --git a/src/app/events/MessageCreate.ts b/src/app/events/MessageCreate.ts index 858035a2..4cb7100f 100644 --- a/src/app/events/MessageCreate.ts +++ b/src/app/events/MessageCreate.ts @@ -1,19 +1,26 @@ import messageCreateOnBountyBoard from './bounty/MessageCreateOnBountyBoard'; import { Message } from 'discord.js'; import { DiscordEvent } from '../types/discord/DiscordEvent'; +import MessageCreateOnDEGEN from './chat/MessageCreateOnDEGEN'; export default class implements DiscordEvent { name = 'messageCreate'; once = false; execute(message: Message): Promise { - if(message.author.bot && message.webhookId === null) return; - const greetings = ['Hello', 'Howdy', 'Hey', 'Go Bankless,', 'Nice to meet you,', 'It\'s great to see you,', 'Ahoy,']; - if (message.content.toLowerCase().match('^.*degen$')) { - message.channel.send({ content: `${greetings[Math.floor(Math.random() * greetings.length)]} ${message.author.username}!` }); + try { + if(message.author.bot && message.webhookId === null) return; + + // DEGEN says hello + MessageCreateOnDEGEN(message).catch(e => { + console.error('ERROR: ', e); + }); + // Run for webhook + messageCreateOnBountyBoard(message).catch(e => { + console.error('ERROR: ', e); + }); + } catch (e) { + console.error(e); } - messageCreateOnBountyBoard(message).catch(e => { - console.error('ERROR: ', e); - }); } } \ No newline at end of file diff --git a/src/app/events/MessageReactionAdd.ts b/src/app/events/MessageReactionAdd.ts index 3dbf61ac..39d3f33e 100644 --- a/src/app/events/MessageReactionAdd.ts +++ b/src/app/events/MessageReactionAdd.ts @@ -7,30 +7,27 @@ export default class implements DiscordEvent { once = false; async execute(reaction: MessageReaction, user: User | PartialUser): Promise { - // When a reaction is received, check if the structure is partial - if (reaction.partial) { - // If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled - try { + try { + // When a reaction is received, check if the structure is partial + if (reaction.partial) { await reaction.fetch(); - } catch (error) { - console.error('Something went wrong when fetching the message: ', error); - return; } - } - - if (user.partial) { - try { - await user.fetch(); - } catch (error) { - console.error('Something is not working for pulling the user, maybe account was removed? lol'); + + if (user.partial) { + try { + await user.fetch(); + } catch (error) { + console.error('Something is not working for pulling the user, maybe account was removed? lol'); + return; + } + } + + if (user.bot) { return; } + await messageReactionAddBounty(reaction, user as User); + } catch (e) { + console.error(e); } - - if (user.bot) { - return; - } - - await messageReactionAddBounty(reaction, user as User); } } diff --git a/src/app/events/Ready.ts b/src/app/events/Ready.ts index 598ff419..78b91867 100644 --- a/src/app/events/Ready.ts +++ b/src/app/events/Ready.ts @@ -10,16 +10,20 @@ export default class implements DiscordEvent { once = true; async execute(client: Client): Promise { - console.log('The Sun will never set on the DAO. Neither will I. DEGEN & Serendipity are ready for service.'); - client.user.setActivity('Going Bankless, Doing the DAO'); - client.guilds.cache.forEach((guild: Guild) => { - console.log(`DEGEN active for: ${guild.id}, ${guild.name}`); - }); - await connect(constants.DB_NAME_DEGEN); - - if (client.guilds.cache.some((guild) => guild.id == discordServerIds.banklessDAO || guild.id == discordServerIds.discordBotGarage)) { - await connect(constants.DB_NAME_BOUNTY_BOARD); - await GuestPassService(client); + try { + console.log('The Sun will never set on the DAO. Neither will I. DEGEN & Serendipity are ready for service.'); + client.user.setActivity('Going Bankless, Doing the DAO'); + client.guilds.cache.forEach((guild: Guild) => { + console.log(`DEGEN active for: ${guild.id}, ${guild.name}`); + }); + await connect(constants.DB_NAME_DEGEN); + + if (client.guilds.cache.some((guild) => guild.id == discordServerIds.banklessDAO || guild.id == discordServerIds.discordBotGarage)) { + await connect(constants.DB_NAME_BOUNTY_BOARD); + await GuestPassService(client); + } + } catch (e) { + console.error(e); } } } \ No newline at end of file diff --git a/src/app/events/UserUpdate.ts b/src/app/events/UserUpdate.ts index a9e379dd..2f392073 100644 --- a/src/app/events/UserUpdate.ts +++ b/src/app/events/UserUpdate.ts @@ -14,16 +14,15 @@ export default class implements DiscordEvent { if (newUser.partial) { newUser = await newUser.fetch(); } + if (oldUser.username !== newUser.username) { + const guildMember = await ServiceUtils.getGuildMemberFromUser(newUser as User, process.env.DISCORD_SERVER_ID); + if (await ServiceUtils.runUsernameSpamFilter(guildMember)) { + return; + } + } } catch (e) { - console.error('Retrieving user partial failed'); + console.error(e); return; } - - if (oldUser.username !== newUser.username) { - const guildMember = await ServiceUtils.getGuildMemberFromUser(newUser as User, process.env.DISCORD_SERVER_ID); - if (await ServiceUtils.runUsernameSpamFilter(guildMember)) { - return; - } - } } } \ No newline at end of file diff --git a/src/app/events/VoiceStateUpdate.ts b/src/app/events/VoiceStateUpdate.ts index c0bcd590..04db95f6 100644 --- a/src/app/events/VoiceStateUpdate.ts +++ b/src/app/events/VoiceStateUpdate.ts @@ -21,6 +21,5 @@ export default class implements DiscordEvent { } catch (e) { console.error(e); } - return; } } diff --git a/src/app/events/chat/MessageCreateOnDEGEN.ts b/src/app/events/chat/MessageCreateOnDEGEN.ts new file mode 100644 index 00000000..3bec1a04 --- /dev/null +++ b/src/app/events/chat/MessageCreateOnDEGEN.ts @@ -0,0 +1,11 @@ +import { Message } from 'discord.js'; + +const MessageCreateOnDEGEN = async (message: Message): Promise => { + const greetings = ['Hello', 'Howdy', 'Hey', 'Go Bankless,', 'Nice to meet you,', 'It\'s great to see you,', 'Ahoy,']; + if (message.content.toLowerCase().match('^.*degen$')) { + message.channel.send({ content: `${greetings[Math.floor(Math.random() * greetings.length)]} ${message.author.username}!` }); + } + return; +}; + +export default MessageCreateOnDEGEN; \ No newline at end of file From 8e8d7ade5fca9883b077c5baaa13323740266ab0 Mon Sep 17 00:00:00 2001 From: Brian Date: Fri, 1 Oct 2021 15:23:05 -0400 Subject: [PATCH 2/2] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 281d8ebf..c1aadade 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "degen", - "version": "1.6.1", + "version": "1.6.2", "description": "Administrative and Utilitarian bot for the Bankless Discord Server.", "main": "app.js", "private": true,