-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
| ] | ||
| } |
| 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. |
| 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 ./ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ services: | |
| volumes: | ||
| - 'postgres_data:/postgres/data' | ||
| ports: | ||
| - 5432 | ||
| - 5432:5432 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| volumes: | ||
| postgres_data: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { Message, Client, User, GuildMember } from 'discord.js'; | ||
| import { 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>; | ||
| } | ||
|
|
||
| export class Bot { | ||
| commands: CommandRegistration[] = []; | ||
| adminCommands: CommandRegistration[] = []; | ||
|
|
||
| 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(); | ||
| this.getByTrigger( | ||
| triggerWithPrefix.substring(matchingPrefix.length), | ||
| ) | ||
| ?.listener(msg, content) | ||
| .catch(err => { | ||
| this.client.emit('error', err); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| registerCommand(command: CommandRegistration) { | ||
| this.commands.push(command); | ||
| } | ||
|
|
||
| registerAdminCommand(command: CommandRegistration) { | ||
| this.adminCommands.push(command); | ||
| } | ||
|
|
||
| getByTrigger(trigger: string): CommandRegistration | undefined { | ||
| const match = (c: CommandRegistration) => c.aliases.includes(trigger); | ||
| return this.commands.find(match) || this.adminCommands.find(match); | ||
| } | ||
|
|
||
| isMod(member: GuildMember | null) { | ||
| return member?.permissions.has('ManageMessages') ?? false; | ||
| } | ||
|
|
||
| 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) { | ||
Gerrit0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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); | ||
| }); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.