Skip to content

Commit

Permalink
refactor: Standardise ClientReady event
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Nov 25, 2023
1 parent 982dd72 commit 00ab038
Show file tree
Hide file tree
Showing 21 changed files with 51 additions and 49 deletions.
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ When you want to highlight a piece of code to display either an addition or a di
Here's our base code:

```js {2,6}
client.once(Events.ClientReady, () => {
console.log('Ready.');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.MessageCreate, message => {
Expand All @@ -203,8 +203,8 @@ client.on(Events.MessageCreate, message => {
To add this feature, use this code:

```js {2,6-8}
client.once(Events.ClientReady, () => {
console.log(`${client.user.tag} ready.`);
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.MessageCreate, message => {
Expand Down
4 changes: 2 additions & 2 deletions code-samples/additional-features/cooldowns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ for (const folder of commandFolders) {
}
}

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async interaction => {
Expand Down
4 changes: 2 additions & 2 deletions code-samples/additional-info/rest-api/14/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const client = new Client({ intents: [GatewayIntentBits.Guilds] });

const trim = (str, max) => (str.length > max ? `${str.slice(0, max - 3)}...` : str);

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async interaction => {
Expand Down
4 changes: 2 additions & 2 deletions code-samples/creating-your-bot/command-deployment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ for (const folder of commandFolders) {
}
}

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async interaction => {
Expand Down
4 changes: 2 additions & 2 deletions code-samples/creating-your-bot/command-handling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ for (const folder of commandFolders) {
}
}

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async interaction => {
Expand Down
4 changes: 2 additions & 2 deletions code-samples/creating-your-bot/initial-files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const { token } = require('./config.json');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.login(token);
4 changes: 2 additions & 2 deletions code-samples/creating-your-bot/slash-commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { token } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// When the client is ready, run this code (only once)
client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`);
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

// Log in to Discord with your client's token
Expand Down
4 changes: 2 additions & 2 deletions code-samples/keyv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { globalPrefix, token } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
const prefixes = new Keyv('sqlite://path/to.sqlite');

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.MessageCreate, async message => {
Expand Down
4 changes: 2 additions & 2 deletions code-samples/popular-topics/canvas/14/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { request } = require('undici');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

const applyText = (canvas, text) => {
Expand Down
4 changes: 2 additions & 2 deletions code-samples/popular-topics/permissions/14/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const { ChannelType, Client, Events, Formatters, GatewayIntentBits, PermissionsB

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, interaction => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessageReactions],
});

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async interaction => {
Expand Down
4 changes: 2 additions & 2 deletions code-samples/popular-topics/reactions/14/basic-reacting.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessageReactions],
});

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async interaction => {
Expand Down
4 changes: 2 additions & 2 deletions code-samples/popular-topics/reactions/14/uncached-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const client = new Client({
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.MessageReactionAdd, async (reaction, user) => {
Expand Down
4 changes: 2 additions & 2 deletions code-samples/sequelize/tags/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Tags = sequelize.define('tags', {
},
});

client.once(Events.ClientReady, () => {
client.once(Events.ClientReady, readyClient => {
/*
* equivalent to: CREATE TABLE tags(
* name VARCHAR(255),
Expand All @@ -41,7 +41,7 @@ client.once(Events.ClientReady, () => {
*/
Tags.sync();

console.log(`Logged in as ${client.user.tag}!`);
console.log(`Logged in as ${readyClient.user.tag}!`);
});

client.on(Events.InteractionCreate, async interaction => {
Expand Down
4 changes: 2 additions & 2 deletions guide/additional-info/rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ const { Client, EmbedBuilder, Events, GatewayIntentBits } = require('discord.js'

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async interaction => {
Expand Down
4 changes: 2 additions & 2 deletions guide/creating-your-bot/event-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ At this point, your `index.js` file has listeners for two events: `ClientReady`
:::: code-group
::: code-group-item ClientReady
```js
client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`);
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
```
:::
Expand Down
8 changes: 5 additions & 3 deletions guide/creating-your-bot/main-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ const { token } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`);
// We use 'readyClient' for the event parameter to keep it separate from the already defined 'client'
// This is important for TypeScript developers as the parameter will be a client that is ready
// with some properties being non-nullable
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

// Log in to Discord with your client's token
Expand Down
4 changes: 2 additions & 2 deletions guide/popular-topics/canvas.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const Canvas = require('@napi-rs/canvas');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, interaction => {
Expand Down
4 changes: 2 additions & 2 deletions guide/popular-topics/reactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions],
});

client.once(Events.ClientReady, () => {
console.log('Ready!');
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, interaction => {
Expand Down
8 changes: 4 additions & 4 deletions guide/sequelize/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ const { Client, Events, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// When the client is ready, run this code (only once)
client.once(Events.ClientReady, () => {
console.log(`Logged in as ${client.user.tag}!`);
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async interaction => {
Expand Down Expand Up @@ -139,9 +139,9 @@ The model mirrors very closely what the database defines. There will be a table
Now that your structure is defined, you need to make sure the model exists in the database. To make sure the bot is ready and all the data you might need has arrived, add this line in your code.

```js {3}
client.once(Events.ClientReady, () => {
client.once(Events.ClientReady, readyClient => {
Tags.sync();
console.log(`Logged in as ${client.user.tag}!`);
console.log(`Logged in as ${readyClient.user.tag}!`);
});
```

Expand Down
8 changes: 4 additions & 4 deletions guide/sequelize/currency.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ const { Users, CurrencyShop } = require('./dbObjects.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });
const currency = new Collection();

client.once(Events.ClientReady, async () => {
console.log(`Logged in as ${client.user.tag}!`);
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.MessageCreate, async message => {
Expand Down Expand Up @@ -259,11 +259,11 @@ This defines the `addBalance()` helper function, since it'll be used quite frequ
### Ready event data sync

```js {2-3}
client.once(Events.ClientReady, async () => {
client.once(Events.ClientReady, async readyClient => {
const storedBalances = await Users.findAll();
storedBalances.forEach(b => currency.set(b.user_id, b));

console.log(`Logged in as ${client.user.tag}!`);
console.log(`Logged in as ${readyClient.user.tag}!`);
});
```

Expand Down

0 comments on commit 00ab038

Please sign in to comment.