refactor: rename events to be consistent with WS names (#6010)

Co-authored-by: Noel <buechler.noel@outlook.com>
This commit is contained in:
Antonio Román
2021-07-03 14:23:53 +02:00
committed by GitHub
parent 7dd1a8da08
commit a11a10525b
13 changed files with 50 additions and 83 deletions

View File

@@ -49,7 +49,7 @@ client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`); console.log(`Logged in as ${client.user.tag}!`);
}); });
client.on('message', message => { client.on('messageCreate', message => {
if (message.content === 'ping') { if (message.content === 'ping') {
message.channel.send('pong'); message.channel.send('pong');
} }

View File

@@ -4,6 +4,8 @@ const Action = require('./Action');
const { Events, InteractionTypes, MessageComponentTypes } = require('../../util/Constants'); const { Events, InteractionTypes, MessageComponentTypes } = require('../../util/Constants');
const Structures = require('../../util/Structures'); const Structures = require('../../util/Structures');
let deprecationEmitted = false;
class InteractionCreateAction extends Action { class InteractionCreateAction extends Action {
handle(data) { handle(data) {
const client = this.client; const client = this.client;
@@ -37,12 +39,25 @@ class InteractionCreateAction extends Action {
return; return;
} }
const interaction = new InteractionType(client, data);
/**
* Emitted when an interaction is created.
* @event Client#interactionCreate
* @param {Interaction} interaction The interaction which was created
*/
client.emit(Events.INTERACTION_CREATE, interaction);
/** /**
* Emitted when an interaction is created. * Emitted when an interaction is created.
* @event Client#interaction * @event Client#interaction
* @param {Interaction} interaction The interaction which was created * @param {Interaction} interaction The interaction which was created
* @deprecated Use {@link Client#interactionCreate} instead
*/ */
client.emit(Events.INTERACTION_CREATE, new InteractionType(client, data)); if (client.emit('interaction', interaction) && !deprecationEmitted) {
deprecationEmitted = true;
process.emitWarning('The interaction event is deprecated. Use interactionCreate instead', 'DeprecationWarning');
}
} }
} }

View File

@@ -3,6 +3,8 @@
const Action = require('./Action'); const Action = require('./Action');
const { Events } = require('../../util/Constants'); const { Events } = require('../../util/Constants');
let deprecationEmitted = false;
class MessageCreateAction extends Action { class MessageCreateAction extends Action {
handle(data) { handle(data) {
const client = this.client; const client = this.client;
@@ -25,10 +27,22 @@ class MessageCreateAction extends Action {
/** /**
* Emitted whenever a message is created. * Emitted whenever a message is created.
* @event Client#message * @event Client#messageCreate
* @param {Message} message The created message * @param {Message} message The created message
*/ */
client.emit(Events.MESSAGE_CREATE, message); client.emit(Events.MESSAGE_CREATE, message);
/**
* Emitted whenever a message is created.
* @event Client#message
* @param {Message} message The created message
* @deprecated Use {@link Client#messageCreate} instead
*/
if (client.emit('message', message) && !deprecationEmitted) {
deprecationEmitted = true;
process.emitWarning('The message event is deprecated. Use messageCreate instead', 'DeprecationWarning');
}
return { message }; return { message };
} }

View File

@@ -255,7 +255,7 @@ exports.Events = {
CHANNEL_DELETE: 'channelDelete', CHANNEL_DELETE: 'channelDelete',
CHANNEL_UPDATE: 'channelUpdate', CHANNEL_UPDATE: 'channelUpdate',
CHANNEL_PINS_UPDATE: 'channelPinsUpdate', CHANNEL_PINS_UPDATE: 'channelPinsUpdate',
MESSAGE_CREATE: 'message', MESSAGE_CREATE: 'messageCreate',
MESSAGE_DELETE: 'messageDelete', MESSAGE_DELETE: 'messageDelete',
MESSAGE_UPDATE: 'messageUpdate', MESSAGE_UPDATE: 'messageUpdate',
MESSAGE_BULK_DELETE: 'messageDeleteBulk', MESSAGE_BULK_DELETE: 'messageDeleteBulk',
@@ -275,7 +275,7 @@ exports.Events = {
VOICE_STATE_UPDATE: 'voiceStateUpdate', VOICE_STATE_UPDATE: 'voiceStateUpdate',
TYPING_START: 'typingStart', TYPING_START: 'typingStart',
WEBHOOKS_UPDATE: 'webhookUpdate', WEBHOOKS_UPDATE: 'webhookUpdate',
INTERACTION_CREATE: 'interaction', INTERACTION_CREATE: 'interactionCreate',
ERROR: 'error', ERROR: 'error',
WARN: 'warn', WARN: 'warn',
DEBUG: 'debug', DEBUG: 'debug',

View File

@@ -42,7 +42,7 @@ client.on('debug', console.log);
client.on('error', m => console.log('debug', new Error(m).stack)); client.on('error', m => console.log('debug', new Error(m).stack));
client.on('reconnecting', m => console.log('reconnecting', m)); client.on('reconnecting', m => console.log('reconnecting', m));
client.on('message', message => { client.on('messageCreate', message => {
if (true) { if (true) {
if (message.content === 'makechann') { if (message.content === 'makechann') {
if (message.channel.guild) { if (message.channel.guild) {
@@ -178,7 +178,7 @@ function chanLoop(channel) {
.catch(console.error); .catch(console.error);
} }
client.on('message', msg => { client.on('messageCreate', msg => {
if (msg.content.startsWith('?raw')) { if (msg.content.startsWith('?raw')) {
msg.channel.send(`\`\`\`${msg.content}\`\`\``); msg.channel.send(`\`\`\`${msg.content}\`\`\``);
} }
@@ -200,7 +200,7 @@ client.on('message', msg => {
let disp, con; let disp, con;
client.on('message', msg => { client.on('messageCreate', msg => {
if (msg.content.startsWith('/play')) { if (msg.content.startsWith('/play')) {
console.log('I am now going to play', msg.content); console.log('I am now going to play', msg.content);
const chan = msg.content const chan = msg.content
@@ -243,7 +243,7 @@ client.on('messageReactionRemove', (reaction, user) => {
reaction.message.channel.send(`${user.username} removed reaction ${reaction.emoji}, count is now ${reaction.count}`); reaction.message.channel.send(`${user.username} removed reaction ${reaction.emoji}, count is now ${reaction.count}`);
}); });
client.on('message', m => { client.on('messageCreate', m => {
if (m.content.startsWith('#reactions')) { if (m.content.startsWith('#reactions')) {
const mID = m.content.split(' ')[1]; const mID = m.content.split(' ')[1];
m.channel.messages.fetch(mID).then(rM => { m.channel.messages.fetch(mID).then(rM => {

View File

@@ -89,7 +89,7 @@ const tests = [
m => m.channel.send('Done!'), m => m.channel.send('Done!'),
]; ];
client.on('message', async message => { client.on('messageCreate', async message => {
if (message.author.id !== owner) return; if (message.author.id !== owner) return;
const match = message.content.match(/^do (.+)$/); const match = message.content.match(/^do (.+)$/);
if (match && match[1] === 'it') { if (match && match[1] === 'it') {

View File

@@ -7,10 +7,9 @@ const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES], intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
shards: process.argv[2], shards: process.argv[2],
shardCount: process.argv[3], shardCount: process.argv[3],
intents: Discord.Intents.NON_PRIVILEGED,
}); });
client.on('message', msg => { client.on('messageCreate', msg => {
if (msg.content.startsWith('?eval') && msg.author.id === '66564597481480192') { if (msg.content.startsWith('?eval') && msg.author.id === '66564597481480192') {
try { try {
const com = eval(msg.content.split(' ').slice(1).join(' ')); const com = eval(msg.content.split(' ').slice(1).join(' '));

View File

@@ -6,7 +6,7 @@ const { Client } = require('../src');
const client = new Client({ intents: ['GUILDS', 'GUILD_MESSAGES'] }); const client = new Client({ intents: ['GUILDS', 'GUILD_MESSAGES'] });
client client
.on('ready', () => console.log('ready')) .on('ready', () => console.log('ready'))
.on('message', async message => { .on('messageCreate', async message => {
try { try {
const templates = await message.guild.fetchTemplates(); const templates = await message.guild.fetchTemplates();
if (!templates.size) { if (!templates.size) {

View File

@@ -9,7 +9,6 @@ const log = (...args) => console.log(process.uptime().toFixed(3), ...args);
const client = new Client({ const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES], intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
shardCount: 2, shardCount: 2,
intents: Discord.Intents.NON_PRIVILEGED,
}); });
client.on('debug', log); client.on('debug', log);
@@ -36,7 +35,7 @@ const commands = {
ping: message => message.channel.send('pong'), ping: message => message.channel.send('pong'),
}; };
client.on('message', message => { client.on('messageCreate', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return; if (!message.content.startsWith(prefix) || message.author.bot) return;
message.content = message.content.replace(prefix, '').trim().split(' '); message.content = message.content.replace(prefix, '').trim().split(' ');

View File

@@ -1,64 +0,0 @@
/* eslint no-console: 0 */
'use strict';
const ytdl = require('ytdl-core');
const auth = require('./auth.js');
const { Client, Intents } = require('../src');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_PRESENCES],
partials: [],
});
client
.login(auth.token)
.then(() => console.log('logged'))
.catch(console.error);
const connections = new Map();
client.on('debug', console.log);
client.on('error', console.log);
process.on('unhandledRejection', console.log);
client.on('presenceUpdate', (a, b) => {
if (b.userID !== '66564597481480192') return;
console.log(a ? a.status : null, b.status, b.user.username);
});
client.on('messageDelete', async m => {
if (m.channel.id !== '80426989059575808') return;
console.log(m.channel.recipient);
console.log(m.channel.partial);
await m.channel.fetch();
console.log('\n\n\n\n');
console.log(m.channel);
});
client.on('message', m => {
if (!m.guild) return;
if (m.author.id !== '66564597481480192') return;
if (m.content.startsWith('/join')) {
const channel = m.guild.channels.cache.get(m.content.split(' ')[1]) ?? m.member.voice.channel;
if (channel && channel.type === 'voice') {
channel.join().then(conn => {
conn.receiver.createStream(m.author, true).on('data', b => console.log(b.toString()));
conn.player.on('error', (...e) => console.log('player', ...e));
if (!connections.has(m.guild.id)) connections.set(m.guild.id, { conn, queue: [] });
m.channel.send('ok!');
conn.play(ytdl('https://www.youtube.com/watch?v=_XXOSf0s2nk', { filter: 'audioonly' }, { passes: 3 }));
});
} else {
m.channel.send('Specify a voice channel!');
}
} else if (m.content.startsWith('#eval') && m.author.id === '66564597481480192') {
try {
const com = eval(m.content.split(' ').slice(1).join(' '));
m.channel.send(com, { code: true });
} catch (e) {
console.log(e);
m.channel.send(String(e), { code: true });
}
}
});

View File

@@ -93,7 +93,7 @@ const tests = [
(m, hook) => hook.send('Done!'), (m, hook) => hook.send('Done!'),
]; ];
client.on('message', async message => { client.on('messageCreate', async message => {
if (message.author.id !== owner) return; if (message.author.id !== owner) return;
const match = message.content.match(/^do (.+)$/); const match = message.content.match(/^do (.+)$/);
const hooks = [ const hooks = [

8
typings/index.d.ts vendored
View File

@@ -680,7 +680,7 @@ declare module 'discord.js' {
CHANNEL_DELETE: 'channelDelete'; CHANNEL_DELETE: 'channelDelete';
CHANNEL_UPDATE: 'channelUpdate'; CHANNEL_UPDATE: 'channelUpdate';
CHANNEL_PINS_UPDATE: 'channelPinsUpdate'; CHANNEL_PINS_UPDATE: 'channelPinsUpdate';
MESSAGE_CREATE: 'message'; MESSAGE_CREATE: 'messageCreate';
MESSAGE_DELETE: 'messageDelete'; MESSAGE_DELETE: 'messageDelete';
MESSAGE_UPDATE: 'messageUpdate'; MESSAGE_UPDATE: 'messageUpdate';
MESSAGE_BULK_DELETE: 'messageDeleteBulk'; MESSAGE_BULK_DELETE: 'messageDeleteBulk';
@@ -700,7 +700,7 @@ declare module 'discord.js' {
VOICE_STATE_UPDATE: 'voiceStateUpdate'; VOICE_STATE_UPDATE: 'voiceStateUpdate';
TYPING_START: 'typingStart'; TYPING_START: 'typingStart';
WEBHOOKS_UPDATE: 'webhookUpdate'; WEBHOOKS_UPDATE: 'webhookUpdate';
INTERACTION_CREATE: 'interaction'; INTERACTION_CREATE: 'interactionCreate';
ERROR: 'error'; ERROR: 'error';
WARN: 'warn'; WARN: 'warn';
DEBUG: 'debug'; DEBUG: 'debug';
@@ -3086,7 +3086,9 @@ declare module 'discord.js' {
guildUpdate: [oldGuild: Guild, newGuild: Guild]; guildUpdate: [oldGuild: Guild, newGuild: Guild];
inviteCreate: [invite: Invite]; inviteCreate: [invite: Invite];
inviteDelete: [invite: Invite]; inviteDelete: [invite: Invite];
/** @deprecated Use messageCreate instead */
message: [message: Message]; message: [message: Message];
messageCreate: [message: Message];
messageDelete: [message: Message | PartialMessage]; messageDelete: [message: Message | PartialMessage];
messageReactionRemoveAll: [message: Message | PartialMessage]; messageReactionRemoveAll: [message: Message | PartialMessage];
messageReactionRemoveEmoji: [reaction: MessageReaction]; messageReactionRemoveEmoji: [reaction: MessageReaction];
@@ -3115,7 +3117,9 @@ declare module 'discord.js' {
userUpdate: [oldUser: User | PartialUser, newUser: User]; userUpdate: [oldUser: User | PartialUser, newUser: User];
voiceStateUpdate: [oldState: VoiceState, newState: VoiceState]; voiceStateUpdate: [oldState: VoiceState, newState: VoiceState];
webhookUpdate: [channel: TextChannel]; webhookUpdate: [channel: TextChannel];
/** @deprecated Use interactionCreate instead */
interaction: [interaction: Interaction]; interaction: [interaction: Interaction];
interactionCreate: [interaction: Interaction];
shardDisconnect: [closeEvent: CloseEvent, shardID: number]; shardDisconnect: [closeEvent: CloseEvent, shardID: number];
shardError: [error: Error, shardID: number]; shardError: [error: Error, shardID: number];
shardReady: [shardID: number, unavailableGuilds: Set<Snowflake> | undefined]; shardReady: [shardID: number, unavailableGuilds: Set<Snowflake> | undefined];

View File

@@ -365,7 +365,7 @@ client.on('messageReactionRemoveAll', async message => {
// This is to check that stuff is the right type // This is to check that stuff is the right type
declare const assertIsMessage: (m: Promise<Message>) => void; declare const assertIsMessage: (m: Promise<Message>) => void;
client.on('message', ({ channel }) => { client.on('messageCreate', ({ channel }) => {
assertIsMessage(channel.send('string')); assertIsMessage(channel.send('string'));
assertIsMessage(channel.send({})); assertIsMessage(channel.send({}));
assertIsMessage(channel.send({ embeds: [] })); assertIsMessage(channel.send({ embeds: [] }));