Skip to content

Commit

Permalink
refactor: rename events to be consistent with WS names (#6010)
Browse files Browse the repository at this point in the history
Co-authored-by: Noel <buechler.noel@outlook.com>
  • Loading branch information
kyranet and iCrawl committed Jul 3, 2021
1 parent 7dd1a8d commit a11a105
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -49,7 +49,7 @@ client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', message => {
client.on('messageCreate', message => {
if (message.content === 'ping') {
message.channel.send('pong');
}
Expand Down
17 changes: 16 additions & 1 deletion src/client/actions/InteractionCreate.js
Expand Up @@ -4,6 +4,8 @@ const Action = require('./Action');
const { Events, InteractionTypes, MessageComponentTypes } = require('../../util/Constants');
const Structures = require('../../util/Structures');

let deprecationEmitted = false;

class InteractionCreateAction extends Action {
handle(data) {
const client = this.client;
Expand Down Expand Up @@ -37,12 +39,25 @@ class InteractionCreateAction extends Action {
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.
* @event Client#interaction
* @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');
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/client/actions/MessageCreate.js
Expand Up @@ -3,6 +3,8 @@
const Action = require('./Action');
const { Events } = require('../../util/Constants');

let deprecationEmitted = false;

class MessageCreateAction extends Action {
handle(data) {
const client = this.client;
Expand All @@ -25,10 +27,22 @@ class MessageCreateAction extends Action {

/**
* Emitted whenever a message is created.
* @event Client#message
* @event Client#messageCreate
* @param {Message} message The created 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 };
}

Expand Down
4 changes: 2 additions & 2 deletions src/util/Constants.js
Expand Up @@ -255,7 +255,7 @@ exports.Events = {
CHANNEL_DELETE: 'channelDelete',
CHANNEL_UPDATE: 'channelUpdate',
CHANNEL_PINS_UPDATE: 'channelPinsUpdate',
MESSAGE_CREATE: 'message',
MESSAGE_CREATE: 'messageCreate',
MESSAGE_DELETE: 'messageDelete',
MESSAGE_UPDATE: 'messageUpdate',
MESSAGE_BULK_DELETE: 'messageDeleteBulk',
Expand All @@ -275,7 +275,7 @@ exports.Events = {
VOICE_STATE_UPDATE: 'voiceStateUpdate',
TYPING_START: 'typingStart',
WEBHOOKS_UPDATE: 'webhookUpdate',
INTERACTION_CREATE: 'interaction',
INTERACTION_CREATE: 'interactionCreate',
ERROR: 'error',
WARN: 'warn',
DEBUG: 'debug',
Expand Down
8 changes: 4 additions & 4 deletions test/random.js
Expand Up @@ -42,7 +42,7 @@ client.on('debug', console.log);
client.on('error', m => console.log('debug', new Error(m).stack));
client.on('reconnecting', m => console.log('reconnecting', m));

client.on('message', message => {
client.on('messageCreate', message => {
if (true) {
if (message.content === 'makechann') {
if (message.channel.guild) {
Expand Down Expand Up @@ -178,7 +178,7 @@ function chanLoop(channel) {
.catch(console.error);
}

client.on('message', msg => {
client.on('messageCreate', msg => {
if (msg.content.startsWith('?raw')) {
msg.channel.send(`\`\`\`${msg.content}\`\`\``);
}
Expand All @@ -200,7 +200,7 @@ client.on('message', msg => {

let disp, con;

client.on('message', msg => {
client.on('messageCreate', msg => {
if (msg.content.startsWith('/play')) {
console.log('I am now going to play', msg.content);
const chan = msg.content
Expand Down Expand Up @@ -243,7 +243,7 @@ client.on('messageReactionRemove', (reaction, user) => {
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')) {
const mID = m.content.split(' ')[1];
m.channel.messages.fetch(mID).then(rM => {
Expand Down
2 changes: 1 addition & 1 deletion test/sendtest.js
Expand Up @@ -89,7 +89,7 @@ const tests = [
m => m.channel.send('Done!'),
];

client.on('message', async message => {
client.on('messageCreate', async message => {
if (message.author.id !== owner) return;
const match = message.content.match(/^do (.+)$/);
if (match && match[1] === 'it') {
Expand Down
3 changes: 1 addition & 2 deletions test/shard.js
Expand Up @@ -7,10 +7,9 @@ const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
shards: process.argv[2],
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') {
try {
const com = eval(msg.content.split(' ').slice(1).join(' '));
Expand Down
2 changes: 1 addition & 1 deletion test/templateCreateGuild.js
Expand Up @@ -6,7 +6,7 @@ const { Client } = require('../src');
const client = new Client({ intents: ['GUILDS', 'GUILD_MESSAGES'] });
client
.on('ready', () => console.log('ready'))
.on('message', async message => {
.on('messageCreate', async message => {
try {
const templates = await message.guild.fetchTemplates();
if (!templates.size) {
Expand Down
3 changes: 1 addition & 2 deletions test/tester1000.js
Expand Up @@ -9,7 +9,6 @@ const log = (...args) => console.log(process.uptime().toFixed(3), ...args);
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
shardCount: 2,
intents: Discord.Intents.NON_PRIVILEGED,
});

client.on('debug', log);
Expand All @@ -36,7 +35,7 @@ const commands = {
ping: message => message.channel.send('pong'),
};

client.on('message', message => {
client.on('messageCreate', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;

message.content = message.content.replace(prefix, '').trim().split(' ');
Expand Down
64 changes: 0 additions & 64 deletions test/voice.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/webhooktest.js
Expand Up @@ -93,7 +93,7 @@ const tests = [
(m, hook) => hook.send('Done!'),
];

client.on('message', async message => {
client.on('messageCreate', async message => {
if (message.author.id !== owner) return;
const match = message.content.match(/^do (.+)$/);
const hooks = [
Expand Down
8 changes: 6 additions & 2 deletions typings/index.d.ts
Expand Up @@ -680,7 +680,7 @@ declare module 'discord.js' {
CHANNEL_DELETE: 'channelDelete';
CHANNEL_UPDATE: 'channelUpdate';
CHANNEL_PINS_UPDATE: 'channelPinsUpdate';
MESSAGE_CREATE: 'message';
MESSAGE_CREATE: 'messageCreate';
MESSAGE_DELETE: 'messageDelete';
MESSAGE_UPDATE: 'messageUpdate';
MESSAGE_BULK_DELETE: 'messageDeleteBulk';
Expand All @@ -700,7 +700,7 @@ declare module 'discord.js' {
VOICE_STATE_UPDATE: 'voiceStateUpdate';
TYPING_START: 'typingStart';
WEBHOOKS_UPDATE: 'webhookUpdate';
INTERACTION_CREATE: 'interaction';
INTERACTION_CREATE: 'interactionCreate';
ERROR: 'error';
WARN: 'warn';
DEBUG: 'debug';
Expand Down Expand Up @@ -3086,7 +3086,9 @@ declare module 'discord.js' {
guildUpdate: [oldGuild: Guild, newGuild: Guild];
inviteCreate: [invite: Invite];
inviteDelete: [invite: Invite];
/** @deprecated Use messageCreate instead */
message: [message: Message];
messageCreate: [message: Message];
messageDelete: [message: Message | PartialMessage];
messageReactionRemoveAll: [message: Message | PartialMessage];
messageReactionRemoveEmoji: [reaction: MessageReaction];
Expand Down Expand Up @@ -3115,7 +3117,9 @@ declare module 'discord.js' {
userUpdate: [oldUser: User | PartialUser, newUser: User];
voiceStateUpdate: [oldState: VoiceState, newState: VoiceState];
webhookUpdate: [channel: TextChannel];
/** @deprecated Use interactionCreate instead */
interaction: [interaction: Interaction];
interactionCreate: [interaction: Interaction];
shardDisconnect: [closeEvent: CloseEvent, shardID: number];
shardError: [error: Error, shardID: number];
shardReady: [shardID: number, unavailableGuilds: Set<Snowflake> | undefined];
Expand Down
2 changes: 1 addition & 1 deletion typings/index.ts
Expand Up @@ -365,7 +365,7 @@ client.on('messageReactionRemoveAll', async message => {
// This is to check that stuff is the right type
declare const assertIsMessage: (m: Promise<Message>) => void;

client.on('message', ({ channel }) => {
client.on('messageCreate', ({ channel }) => {
assertIsMessage(channel.send('string'));
assertIsMessage(channel.send({}));
assertIsMessage(channel.send({ embeds: [] }));
Expand Down

0 comments on commit a11a105

Please sign in to comment.