Skip to content

Commit

Permalink
bump(1.0.4): added GuildCreate logging
Browse files Browse the repository at this point in the history
  • Loading branch information
GalvinPython committed Jul 2, 2024
1 parent 1121327 commit 4a3db7b
Show file tree
Hide file tree
Showing 10 changed files with 1,924 additions and 119 deletions.
115 changes: 115 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"extends": "eslint:recommended",
"env": {
"node": true,
"es6": true
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": "latest"
},
"rules": {
"arrow-spacing": [
"warn",
{
"before": true,
"after": true
}
],
"brace-style": [
"error",
"stroustrup",
{
"allowSingleLine": true
}
],
"comma-dangle": [
"error",
"always-multiline"
],
"comma-spacing": "error",
"comma-style": "error",
"curly": [
"error",
"multi-line",
"consistent"
],
"dot-location": [
"error",
"property"
],
"handle-callback-err": "off",
"indent": [
"error",
"tab"
],
"keyword-spacing": "error",
"max-nested-callbacks": [
"error",
{
"max": 4
}
],
"max-statements-per-line": [
"error",
{
"max": 2
}
],
"no-console": "off",
"no-empty-function": "error",
"no-floating-decimal": "error",
"no-inline-comments": "error",
"no-lonely-if": "error",
"no-multi-spaces": "error",
"no-multiple-empty-lines": [
"error",
{
"max": 2,
"maxEOF": 1,
"maxBOF": 0
}
],
"no-shadow": [
"error",
{
"allow": [
"err",
"resolve",
"reject"
]
}
],
"no-trailing-spaces": [
"error"
],
"no-var": "error",
"object-curly-spacing": [
"error",
"always"
],
"prefer-const": "error",
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
],
"space-before-blocks": "error",
"space-before-function-paren": [
"error",
{
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}
],
"space-in-parens": "error",
"space-infix-ops": "error",
"space-unary-ops": "error",
"spaced-comment": "error",
"yoda": "error"
}
}
16 changes: 8 additions & 8 deletions bot/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { CommandInteraction } from 'discord.js';

interface Command {
data: {
options: any[];
options: never[];
name: string;
description: string;
integration_types: number[];
Expand All @@ -15,7 +15,7 @@ interface Command {
execute: (interaction: CommandInteraction) => Promise<void>;
}

const commands: { [key: string]: Command } = {
const commands: Record<string, Command> = {
ping: {
data: {
options: [],
Expand All @@ -24,7 +24,7 @@ const commands: { [key: string]: Command } = {
integration_types: [0, 1],
contexts: [0, 1, 2],
},
execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise<any>; client: { ws: { ping: any; }; }; }) => {
execute: async (interaction: CommandInteraction) => {
await interaction
.reply({
ephemeral: false,
Expand All @@ -41,7 +41,7 @@ const commands: { [key: string]: Command } = {
integration_types: [0, 1],
contexts: [0, 1, 2],
},
execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise<any>; }) => {
execute: async (interaction: CommandInteraction) => {
await client.application?.commands?.fetch().catch(console.error);
const chat_commands = client.application?.commands.cache.map((a) => {
return `</${a.name}:${a.id}>: ${a.description}`;
Expand All @@ -62,7 +62,7 @@ const commands: { [key: string]: Command } = {
integration_types: [0, 1],
contexts: [0, 1, 2],
},
execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise<any>; }) => {
execute: async (interaction: CommandInteraction) => {
await interaction
.reply({
ephemeral: true,
Expand All @@ -79,7 +79,7 @@ const commands: { [key: string]: Command } = {
integration_types: [0, 1],
contexts: [0, 1, 2],
},
execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise<any>; }) => {
execute: async (interaction: CommandInteraction) => {
await interaction
.reply({
ephemeral: false,
Expand All @@ -98,7 +98,7 @@ const commands: { [key: string]: Command } = {
integration_types: [0, 1],
contexts: [0, 1, 2],
},
execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise<any>; }) => {
execute: async (interaction: CommandInteraction) => {
const heap = heapStats();
Bun.gc(false);
await interaction
Expand All @@ -122,7 +122,7 @@ const commands: { [key: string]: Command } = {
// Convert commands to a Map
const commandsMap = new Map<string, Command>();
for (const key in commands) {
if (commands.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(commands, key)) {
const command = commands[key];
console.log('loading ' + key);
commandsMap.set(key, command);
Expand Down
28 changes: 23 additions & 5 deletions bot/events/guildAdd.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Events, EmbedBuilder } from 'discord.js';
import { Events, EmbedBuilder, TextChannel } from 'discord.js';
import client from '../index';

client.on(Events.GuildCreate, async guild => {
// Fetch the owner of the guild
try {
// Fetch the owner of the guild
const owner = await guild.fetchOwner();

// Create an embed with the guild's information
const embed = new EmbedBuilder()
const embedOwner = new EmbedBuilder()
.setTitle(`Hi! Thanks for adding me to your server: **${guild.name}**.`)
.setDescription('Hello! Please make sure that I have admin permissions enabled to function correctly.')
.setThumbnail(guild.iconURL())
Expand All @@ -19,10 +19,28 @@ client.on(Events.GuildCreate, async guild => {

// Send a message to the owner
await owner.send({
embeds: [embed]
embeds: [embedOwner]
});

console.log(`Sent a welcome message to the owner of the guild: ${guild.name}`);

// Send a message to the logging channel
const embedLoggingChannel = new EmbedBuilder()
.setTitle("New Guild Added!")
.setColor('Blurple')
.setDescription(`New guild added: **${guild.name}**`)
.setThumbnail(guild.iconURL())
.addFields({ name: 'Guild ID', value: guild.id, inline: true })
.addFields({ name: 'Members', value: guild.memberCount.toLocaleString(), inline: true })
.addFields({ name: 'Owner', value: owner.user.tag, inline: true })

const loggingChannelId = process.env.DISCORD_LOGGING_CHANNEL;
if (!loggingChannelId) {
throw new Error('DISCORD_LOGGING_CHANNEL environment variable is not defined.');
}

const channel = await client.channels.fetch(loggingChannelId) as TextChannel;
await channel?.send({ embeds: [embedLoggingChannel] });

} catch (error) {
console.error(`Could not fetch the owner or send a message: ${error}`);
}
Expand Down
27 changes: 11 additions & 16 deletions bot/events/ready.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ActivityType, Events } from 'discord.js';
import client from '../index';

// Log into the bot
client.once(Events.ClientReady, async (bot) => {
console.log(`Ready! Logged in as ${bot.user?.tag}`);
bot.user.setPresence({
// update the bot's presence
function updatePresence() {
if (!client?.user) return;
client.user.setPresence({
activities: [
{
name: `Publishing from ${client.guilds.cache.size} servers.`,
Expand All @@ -13,18 +13,13 @@ client.once(Events.ClientReady, async (bot) => {
],
status: 'online',
});
}

// Log into the bot
client.once(Events.ClientReady, async (bot) => {
console.log(`Ready! Logged in as ${bot.user?.tag}`);
updatePresence();
});

// Update the server count in the status every minute
setInterval(() => {
if (!client?.user) return;
client.user.setPresence({
activities: [
{
name: `Publishing from ${client.guilds.cache.size} servers.`,
type: ActivityType.Custom,
},
],
status: 'online',
});
}, 60000);
setInterval(updatePresence, 60000);
10 changes: 4 additions & 6 deletions bot/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Check if DISCORD_TOKEN has been provided as an environment variable, and is a valid regex pattern
const discordToken: string | undefined = process.env?.DISCORD_TOKEN

if (!discordToken || discordToken === 'YOUR_TOKEN_HERE') throw 'You MUST provide a discord token in .env!'
if (!discordToken || discordToken === 'YOUR_TOKEN_HERE') throw new Error('You MUST provide a discord token in .env!');

// If it has, run the bot
import { Client, GatewayIntentBits, REST, Routes } from 'discord.js';
Expand Down Expand Up @@ -29,16 +29,14 @@ const data: any = await rest.put(
},
);

console.log(
`Successfully reloaded ${data.length} application (/) commands.`,
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);

client.login(discordToken);

export default client

// Import events
const getEvents = await fs.readdir('bot/events');
for await (const file of getEvents) {
await Promise.all(getEvents.map(async (file) => {
await import('./events/' + file);
}
}));
4 changes: 2 additions & 2 deletions bot/log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBuilder } from 'discord.js';
import { EmbedBuilder, TextChannel } from 'discord.js';
import client from '.';

const targetChannel = process.env.DISCORD_LOGGING_CHANNEL;
Expand All @@ -12,7 +12,7 @@ export default async function (success: boolean, msg: string) {
.setTitle(success ? 'Message successfully sent' : 'Error');

try {
const channel = await client.channels.fetch(targetChannel);
const channel = await client.channels.fetch(targetChannel) as TextChannel;
await channel?.send({ embeds: [embed] });
} catch (error) {
console.error('Error sending message:', error);
Expand Down
Binary file modified bun.lockb
Binary file not shown.
10 changes: 10 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @ts-check

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
...tseslint.configs.stylistic,
);
Loading

0 comments on commit 4a3db7b

Please sign in to comment.