-
Notifications
You must be signed in to change notification settings - Fork 27
Discord v14, farewell Cookiecord #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #!/usr/bin/env sh | ||
| . "$(dirname -- "$0")/_/husky.sh" | ||
|
|
||
| npx pretty-quick --staged |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| { | ||
| "typescript.tsdk": "node_modules/typescript/lib" | ||
| "typescript.tsdk": "node_modules/typescript/lib", | ||
| "cSpell.words": [ | ||
| "algoliasearch", | ||
| "autorole", | ||
| "Cooldown", | ||
| "leaderboard", | ||
| "twoslash", | ||
| "twoslasher" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # 2022-11-19 | ||
|
|
||
| - Updated to Discord.js 14, removed Cookiecord to prevent future delays in updating versions. | ||
| - The bot will now react on the configured autorole messages to indicate available roles. | ||
| - Unhandled rejections will now only be ignored if `NODE_ENV` is set to `production`. | ||
| - Removed admin `checkThreads` command as using it would result in the bot checking for closed threads twice as often until restarted. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| FROM node:16.14.0-alpine | ||
| FROM node:16.18.1-alpine | ||
| WORKDIR /usr/src/app | ||
|
|
||
| COPY yarn.lock ./ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ services: | |
| volumes: | ||
| - 'postgres_data:/postgres/data' | ||
| ports: | ||
| - 5432 | ||
| - 5432:5432 | ||
|
|
||
| volumes: | ||
| postgres_data: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { Message, Client, User, GuildMember } from 'discord.js'; | ||
| import { botAdmins, prefixes, trustedRoleId } from './env'; | ||
|
|
||
| export interface CommandRegistration { | ||
| aliases: string[]; | ||
Gerrit0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| description?: string; | ||
| listener: (msg: Message, content: string) => Promise<void>; | ||
| } | ||
|
|
||
| interface Command { | ||
| admin: boolean; | ||
| aliases: string[]; | ||
| description?: string; | ||
| listener: (msg: Message, content: string) => Promise<void>; | ||
| } | ||
|
|
||
| export class Bot { | ||
| commands = new Map<string, Command>(); | ||
|
|
||
| constructor(public client: Client<true>) { | ||
| client.on('messageCreate', msg => { | ||
| const triggerWithPrefix = msg.content.split(/\s/)[0]; | ||
| const matchingPrefix = prefixes.find(p => | ||
| triggerWithPrefix.startsWith(p), | ||
| ); | ||
| if (matchingPrefix) { | ||
Gerrit0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const content = msg.content | ||
| .substring(triggerWithPrefix.length + 1) | ||
| .trim(); | ||
|
|
||
| const command = this.getByTrigger( | ||
| triggerWithPrefix.substring(matchingPrefix.length), | ||
| ); | ||
|
|
||
| if (!command || (command.admin && !this.isAdmin(msg.author))) { | ||
| return; | ||
| } | ||
| command.listener(msg, content).catch(err => { | ||
| this.client.emit('error', err); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| registerCommand(registration: CommandRegistration) { | ||
| const command: Command = { | ||
| ...registration, | ||
| admin: false, | ||
| }; | ||
| for (const a of command.aliases) { | ||
| this.commands.set(a, command); | ||
| } | ||
| } | ||
|
|
||
| registerAdminCommand(registration: CommandRegistration) { | ||
| const command: Command = { | ||
| ...registration, | ||
| admin: true, | ||
| }; | ||
| for (const a of command.aliases) { | ||
| this.commands.set(a, command); | ||
| } | ||
| } | ||
|
|
||
| getByTrigger(trigger: string): Command | undefined { | ||
| return this.commands.get(trigger); | ||
| } | ||
|
|
||
| isMod(member: GuildMember | null) { | ||
| return member?.permissions.has('ManageMessages') ?? false; | ||
| } | ||
|
|
||
| isAdmin(user: User) { | ||
| return botAdmins.includes(user.id); | ||
| } | ||
|
|
||
| getTrustedMemberError(msg: Message) { | ||
| if (!msg.guild || !msg.member || !msg.channel.isTextBased()) { | ||
| return ":warning: you can't use that command here."; | ||
| } | ||
|
|
||
| if ( | ||
| !msg.member.roles.cache.has(trustedRoleId) && | ||
| !msg.member.permissions.has('ManageMessages') | ||
| ) { | ||
| return ":warning: you don't have permission to use that command."; | ||
| } | ||
| } | ||
|
|
||
| async getTargetUser(msg: Message): Promise<User | undefined> { | ||
| const query = msg.content.split(/\s/)[1]; | ||
|
|
||
| const mentioned = msg.mentions.members?.first()?.user; | ||
| if (mentioned) return mentioned; | ||
|
|
||
| if (!query) return; | ||
|
|
||
| // Search by ID | ||
| const queriedUser = await this.client.users | ||
| .fetch(query) | ||
| .catch(() => undefined); | ||
| if (queriedUser) return queriedUser; | ||
|
|
||
| // Search by name, likely a better way to do this... | ||
| for (const user of this.client.users.cache.values()) { | ||
| if (user.tag === query || user.username === query) { | ||
| return user; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,63 +1,69 @@ | ||
| import { token, botAdmins, prefixes } from './env'; | ||
| import CookiecordClient from 'cookiecord'; | ||
| import { Intents } from 'discord.js'; | ||
| import { Client, GatewayIntentBits, Partials } from 'discord.js'; | ||
| import { Bot } from './bot'; | ||
| import { getDB } from './db'; | ||
| import { token } from './env'; | ||
| import { hookLog } from './log'; | ||
|
|
||
| import { AutoroleModule } from './modules/autorole'; | ||
| import { EtcModule } from './modules/etc'; | ||
| import { HelpThreadModule } from './modules/helpthread'; | ||
| import { PlaygroundModule } from './modules/playground'; | ||
| import { RepModule } from './modules/rep'; | ||
| import { TwoslashModule } from './modules/twoslash'; | ||
| import { HelpModule } from './modules/help'; | ||
| import { SnippetModule } from './modules/snippet'; | ||
| import { HandbookModule } from './modules/handbook'; | ||
| import { ModModule } from './modules/mod'; | ||
| import { autoroleModule } from './modules/autorole'; | ||
| import { etcModule } from './modules/etc'; | ||
| import { handbookModule } from './modules/handbook'; | ||
| import { helpModule } from './modules/help'; | ||
| import { modModule } from './modules/mod'; | ||
| import { playgroundModule } from './modules/playground'; | ||
| import { repModule } from './modules/rep'; | ||
| import { twoslashModule } from './modules/twoslash'; | ||
| import { snippetModule } from './modules/snippet'; | ||
| import { helpThreadModule } from './modules/helpthread'; | ||
|
|
||
| const client = new CookiecordClient( | ||
| { | ||
| botAdmins, | ||
| prefix: prefixes, | ||
| const client = new Client({ | ||
| partials: [ | ||
| Partials.Reaction, | ||
| Partials.Message, | ||
| Partials.User, | ||
| Partials.Channel, | ||
| ], | ||
| allowedMentions: { | ||
| parse: ['users', 'roles'], | ||
| }, | ||
| { | ||
| partials: ['REACTION', 'MESSAGE', 'USER', 'CHANNEL'], | ||
| allowedMentions: { | ||
| parse: ['users', 'roles'], | ||
| }, | ||
| intents: new Intents([ | ||
| 'GUILDS', | ||
| 'GUILD_MESSAGES', | ||
| 'GUILD_MEMBERS', | ||
| 'GUILD_MESSAGE_REACTIONS', | ||
| 'DIRECT_MESSAGES', | ||
| ]), | ||
| }, | ||
| ).setMaxListeners(Infinity); | ||
|
|
||
| for (const mod of [ | ||
| AutoroleModule, | ||
| EtcModule, | ||
| HelpThreadModule, | ||
| PlaygroundModule, | ||
| RepModule, | ||
| TwoslashModule, | ||
| HelpModule, | ||
| SnippetModule, | ||
| HandbookModule, | ||
| ModModule, | ||
| ]) { | ||
| client.registerModule(mod); | ||
| } | ||
| intents: [ | ||
| GatewayIntentBits.Guilds, | ||
| GatewayIntentBits.GuildMessages, | ||
| GatewayIntentBits.GuildMembers, | ||
| GatewayIntentBits.GuildMessageReactions, | ||
| GatewayIntentBits.DirectMessages, | ||
| GatewayIntentBits.MessageContent, | ||
| ], | ||
| }).setMaxListeners(Infinity); | ||
|
|
||
| getDB(); // prepare the db for later | ||
| getDB().then(() => client.login(token)); | ||
|
|
||
| client.login(token); | ||
ckiee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| client.on('ready', () => { | ||
| client.on('ready', async () => { | ||
| const bot = new Bot(client); | ||
| console.log(`Logged in as ${client.user?.tag}`); | ||
| hookLog(client); | ||
| await hookLog(client); | ||
|
|
||
| for (const mod of [ | ||
| autoroleModule, | ||
| etcModule, | ||
| helpThreadModule, | ||
| playgroundModule, | ||
| repModule, | ||
| twoslashModule, | ||
| helpModule, | ||
| snippetModule, | ||
| handbookModule, | ||
| modModule, | ||
| ]) { | ||
| await mod(bot); | ||
| } | ||
| }); | ||
|
|
||
| process.on('unhandledRejection', e => { | ||
| console.error('Unhandled rejection', e); | ||
| client.on('error', error => { | ||
| console.error(error); | ||
| }); | ||
|
|
||
| if (process.env.NODE_ENV === 'production') { | ||
| process.on('unhandledRejection', e => { | ||
| console.error('Unhandled rejection', e); | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not strictly necessary, but useful if you want to run the bot locally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this because it was giving me issues running this locally - seems like maybe we have mutually incompatible setups of some sort?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting... according to https://docs.docker.com/compose/compose-file/compose-file-v3/#ports, without
:5432Docker picks a random port, which would work around your port in use issue. I suspect the port was actually in use.