Skip to content

Commit

Permalink
Member introduction to #general-chat and some dynamic commands and ev…
Browse files Browse the repository at this point in the history
…ents handling.
  • Loading branch information
Solysh committed May 4, 2022
1 parent 5f79b00 commit f0dc8e2
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 35 deletions.
7 changes: 4 additions & 3 deletions deploy-commands.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Require dotenv
require('dotenv').config();
const fs = require('node:fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./config.json');

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
Expand All @@ -11,8 +12,8 @@ for (const file of commandFiles) {
commands.push(command.data.toJSON());
}

const rest = new REST({ version: '9' }).setToken(token);
const rest = new REST({ version: '9' }).setToken(process.env.DISCORD_TOKEN);

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
rest.put(Routes.applicationGuildCommands(process.env.clientId, process.env.guildId), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);
2 changes: 1 addition & 1 deletion events/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
name: 'ready',
once: true,
execute(client) {
console.log(`Ready! Logged in as ${client.user.tag}`);
console.log(`Ready! Logged in as ${client.user.tag}!`);
},
};
109 changes: 79 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,103 @@
// Require dotenv
require('dotenv').config();

// Require node:fs, Node's native file system
const fs = require('node:fs');

// Require the neccesary discord.js classes
const { Client, Intents } = require('discord.js');
const { Client, Collection, Intents } = require('discord.js');

// Create a new client instance
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MEMBERS,
],
partials: ["GUILD_MEMBER"]
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MEMBERS,
],
partials: ['GUILD_MEMBER'],
});

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const event = require(`./events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}

client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}

client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;

const command = client.commands.get(interaction.commandName);

if (!command) return;

try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});

// Testing message creation
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (!message.guild) return;
if (message.channel.id === '928175164896718909') {
const messages = ['Hey there', 'Hello', 'Greetings', 'Welcome'];
const randomMessage = messages[Math.floor(Math.random() * messages.length)];
client.channels.cache.get('928175164896718909').send(randomMessage + ', ' + message.author.toString() +
'! Please be sure to read the Welcome FAQ over at <#722901843105153095> and let us know if you have any questions!');
}
});

// Detect new Message in #player-introductions
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (!message.guild) return;
if (!message.member.roles.cache.some(r => r.name === 'Initiate')) return;
if (message.channel.id === '722903689790095421') {
if (message.content.includes(" ")) {
await message.guild.roles.fetch()
let requiredRole = message.guild.roles.cache.find(r => r.name === 'Initiate')
let desiredRole = message.guild.roles.cache.find(r => r.name === 'Verified')
let extraSystems = message.guild.roles.cache.find(r => r.id === '762528156040953856')
let extraCurrentGames = message.guild.roles.cache.find(r => r.id === '762519220726399046')

message.member.roles.add(desiredRole)
message.member.roles.add(extraSystems)
message.member.roles.add(extraCurrentGames)
message.member.roles.remove(requiredRole)
//client.channels.cache.get('722903613328064542').send('Hello there!');
return message.reply({ content: "Nice to meet you!" });
}
}
if (message.author.bot) return;
if (!message.guild) return;
if (!message.member.roles.cache.some(r => r.name === 'Initiate')) return;
if (message.channel.id === '722903689790095421') {
if (message.content.includes(' ')) {
await message.guild.roles.fetch();
const requiredRole = message.guild.roles.cache.find(r => r.name === 'Initiate');
const desiredRole = message.guild.roles.cache.find(r => r.name === 'Verified');
const extraSystems = message.guild.roles.cache.find(r => r.id === '762528156040953856');
const extraCurrentGames = message.guild.roles.cache.find(r => r.id === '762519220726399046');

message.member.roles.add(desiredRole);
message.member.roles.add(extraSystems);
message.member.roles.add(extraCurrentGames);
message.member.roles.remove(requiredRole);

const messages = ['Hey there', 'Hello', 'Greetings', 'Welcome'];
const randomMessage = messages[Math.floor(Math.random() * messages.length)];
client.channels.cache.get('722903613328064542').send(randomMessage + ', ' + message.author.toString() +
'! Please be sure to read the Welcome FAQ over at <#722901843105153095> and let us know if you have any questions!');
return message.reply({ content: 'Nice to meet you!' });
}
}
});

// Remove old introductions of members who have left the server
//client.on('guildMemberRemove', async member => {
// client.on('guildMemberRemove', async member => {
// member.guild.channels.cache.forEach(channel => {
// const fetched = await channel.messages.fetch({limit: 100});
// const filtered = fetched.filter(m => m.author.id === member.user.id);
// await message.channel.bulkDelete(filtered);
// });
//});
// });

// Remove introductions when members leave the server

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"wokcommands": "^1.5.3"
},
"name": "discord-bot",
"version": "0.0.1",
"version": "0.1.1",
"main": "index.js",
"keywords": [],
"author": "",
Expand Down

0 comments on commit f0dc8e2

Please sign in to comment.