Skip to content

Commit

Permalink
Merge pull request #7 from Project-Coda/dev
Browse files Browse the repository at this point in the history
1.1.0
  • Loading branch information
ikifar2012 authored Jul 11, 2022
2 parents f7019d1 + 639eae6 commit 76095fd
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 29 deletions.
3 changes: 0 additions & 3 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ services:
db:
image: mariadb
restart: always
command:
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
volumes:
- data:/var/lib/mysql
environment:
Expand Down
3 changes: 2 additions & 1 deletion commands/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module.exports = {
description: usernames.join('\n'),
color: '#19ebfe',
},
)],
)], ephemeral: true,
});

}
Expand Down Expand Up @@ -197,6 +197,7 @@ module.exports = {
}
catch (error) {
console.log(error);
embedcreator.sendError(error);
return interaction.reply({
embeds: [ embedcreator.setembed(
{
Expand Down
16 changes: 12 additions & 4 deletions commands/reaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { SlashCommandBuilder } = require('@discordjs/builders');
const mariadb = require('../db.js');
const env = require('../env.js');
const embedcreator = require('../embed.js');
const emojiUnicode = require('emoji-unicode');
module.exports = {
data: new SlashCommandBuilder()
.setName('reaction')
Expand Down Expand Up @@ -75,7 +76,7 @@ module.exports = {
emojiname = emoji.split(':')[1].split('>')[0];
}
else {
emojiname = emoji;
emojiname = emojiUnicode(emoji);
}
// extract the channel id from the message link
const channelId = messageLink.split('/')[5];
Expand Down Expand Up @@ -105,10 +106,12 @@ module.exports = {
const message = await channel.messages.fetch(messageId);
// Add to roles table if it doesn't exist
db = await mariadb.getConnection();
await db.query('SET NAMES utf8mb4');
await db.query('INSERT INTO roles (id, emoji, raw_emoji, message_id, channel_id) VALUES (?, ?, ?, ?, ?)', [roleid, emojiname, emoji, messageId, channelId]);
db.end();
message.react(emoji).then(() => {
console.log(`Added ${emojiname} to database`);
console.log(`Added ${emoji} to database`);
embedcreator.log(`Added role ${roleName} to message ${messageLink}`);
interaction.reply({
embeds: [ embedcreator.setembed(
{
Expand All @@ -123,6 +126,7 @@ module.exports = {
},
).catch(err => {
console.log(err);
embedcreator.sendError(`Error adding role ${roleName} to message ${messageLink}`);
interaction.reply({
embeds: [ embedcreator.setembed(
{
Expand All @@ -138,6 +142,7 @@ module.exports = {
}
catch (err) {
console.log(err);
embedcreator.sendError(`${err.text}`);
interaction.reply({
embeds: [ embedcreator.setembed(
{
Expand Down Expand Up @@ -202,11 +207,12 @@ module.exports = {
db.end();
// lookup emoji id in guild
message.reactions.cache.get(emojiId).remove().then(() => {
console.log(`Removed ${roleName} from database`);
embedcreator.log(`Removed ${roleName} from the database`);
console.log(`Removed ${roleName} from the database`);
interaction.reply({
embeds: [ embedcreator.setembed(
{
title: 'Added Role',
title: 'Removed Role',
description: `Removed ${roleName} from the database`,
color: '#2ecc71',
},
Expand All @@ -216,6 +222,7 @@ module.exports = {
},
).catch(err => {
console.log(err);
embedcreator.sendError(`Error removing ${roleName} from database`);
interaction.reply({
embeds: [ embedcreator.setembed(
{
Expand All @@ -231,6 +238,7 @@ module.exports = {
}
catch (err) {
console.log(err);
embedcreator.sendError(`${err.text}`);
interaction.reply({
embeds: [ embedcreator.setembed(
{
Expand Down
1 change: 1 addition & 0 deletions commands/vc.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ module.exports = {
}
catch (error) {
console.log(error);
embedcreator.sendError(error);
interaction.reply({
embeds: [ embedcreator.setembed(
{
Expand Down
18 changes: 18 additions & 0 deletions commands/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { SlashCommandBuilder } = require('@discordjs/builders');
const embedcreator = require('../embed.js');
const pkg = require('../package.json');
module.exports = {
data: new SlashCommandBuilder()
.setName('version')
.setDescription('Get the version of the bot'),
async execute(interaction) {
interaction.reply({
embeds: [embedcreator.setembed({
title: 'Version Information',
description: `**Coda Utilities\nVersion:** ${pkg.version}`,
color: '#19ebfe',
})], ephemeral: true,
});
embedcreator.log(`${interaction.member.user} used the version command.`);
},
};
23 changes: 22 additions & 1 deletion embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,25 @@ var setembed = function(opts){
};
return merge (embed, opts);
};
module.exports = { setembed };
var sendError = function(message){
try {
const embed = setembed({
title: 'Error',
description: `${message}`,
color: '#e74c3c',
});
global.client.channels.cache.get(env.discord.logs_channel).send({ embeds: [embed] });
}
catch (err) {
console.log(err);
}
};
var log = function(message){
const embed = setembed({
title: 'Log',
description: `${message}`,
color: '#19ebfe',
});
global.client.channels.cache.get(env.discord.logs_channel).send({ embeds: [embed] });
};
module.exports = { setembed, sendError, log };
83 changes: 65 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { Routes } = require('discord-api-types/v9');
const fs = require('node:fs');
const mariadb = require('./db.js');
const greet = require('./utilities/greet.js');
const embedcreator = require('./embed.js');
const emojiUnicode = require('emoji-unicode');
global.client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_VOICE_STATES],
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
Expand Down Expand Up @@ -59,6 +61,7 @@ const rest = new REST({ version: '9' }).setToken(env.discord.token);
}
catch (error) {
console.error(error);
embedcreator.sendError(error);
}
})();

Expand Down Expand Up @@ -88,30 +91,46 @@ global.client.on('messageReactionAdd', async (reaction, user) => {
}
catch (error) {
console.error('Something went wrong when fetching the message:', error);
embedcreator.sendError(error);
return;
}
}
const message = reaction.message;
const channel = message.channel;
const guild = channel.guild;
const emoji = String(reaction.emoji.name);
console.log(reaction.emoji);
if (reaction.emoji.id) {
emoji = reaction.emoji.name;
}
else {
emoji = emojiUnicode(reaction.emoji.name);
}
console.log(emoji);
// query db for role
try {
db = await mariadb.getConnection();
const role = await db.query(`SELECT * FROM roles WHERE raw_emoji = '${emoji}'`);
const role = await db.query('SELECT * FROM roles WHERE emoji = ? AND message_id = ?', [emoji, message.id]);
db.end();
const roleId = String(role[0].id);
console.log(role);
console.log(roleId);
const roleName = await guild.roles.cache.get(roleId).name;
const member = guild.members.cache.get(user.id);
if (member) {
member.roles.add(roleId);
try {
member.roles.add(roleId);
}
catch (error) {
console.error(error);
embedcreator.sendError(error);
}
}
console.log(`${user.username} reacted to ${roleName} in ${guild.name} with ${emoji}`);
embedcreator.log(`${user} reacted to ${roleName} in ${guild.name} with ${reaction.emoji}`);
}
catch (error) {
console.error(error);
embedcreator.sendError(error);
}
},
);
Expand All @@ -123,24 +142,52 @@ global.client.on('messageReactionRemove', async (reaction, user) => {
}
catch (error) {
console.error('Something went wrong when fetching the message:', error);
embedcreator.sendError(error);
return;
}
}
const message = reaction.message;
const channel = message.channel;
const guild = channel.guild;
const emoji = reaction.emoji.name;
// query db for role
db = await mariadb.getConnection();
const role = await db.query('SELECT * FROM roles WHERE emoji = ? AND message_id = ?', [emoji, message.id]);
db.end();
if (role.length === 0) return;
const roleId = String(role[0].id);
const member = guild.members.cache.get(user.id);
const roleName = guild.roles.cache.get(roleId).name;
if (member) {
member.roles.remove(roleId);
try {
const message = reaction.message;
const channel = message.channel;
const guild = channel.guild;
console.log(reaction.emoji);
if (reaction.emoji.id) {
emoji = reaction.emoji.name;
}
else {
emoji = emojiUnicode(reaction.emoji.name);
}
console.log(emoji);
// query db for role
db = await mariadb.getConnection();
const role = await db.query('SELECT * FROM roles WHERE emoji = ? AND message_id = ?', [emoji, message.id]);
db.end();
if (role.length === 0) return;
const roleId = String(role[0].id);
const member = guild.members.cache.get(user.id);
const roleName = guild.roles.cache.get(roleId).name;
if (member) {
try {
member.roles.remove(roleId);
}
catch (error) {
console.error(error);
}
}

console.log(`${user.username} un-reacted to ${roleName} in ${guild.name} with ${emoji}`);
embedcreator.log(`${user} un-reacted to ${roleName} in ${guild.name} with ${reaction.emoji}`);
}
console.log(`${user.username} un-reacted to ${roleName} in ${guild.name} with ${emoji}`);
catch (error) {
console.error(error);
// send error to discord
embedcreator.sendError(error);
}
},
);
process.on('unhandledRejection', error => {
console.error(error);
// send error to discord
embedcreator.sendError(error);
},
);
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "coda-utilities",
"version": "1.0.2",
"version": "1.1.0",
"description": "A general utilities bot for Coda",
"main": "index.js",
"scripts": {
"test": "nodemon ."
"test": "nodemon .",
"start": "node index.js"
},
"author": "ikifar2012",
"license": "ISC",
Expand All @@ -17,6 +18,7 @@
"@discordjs/rest": "^0.5.0",
"discord-api-types": "^0.36.1",
"discord.js": "^13.8.1",
"emoji-unicode": "^2.0.1",
"lodash": "^4.17.21",
"mariadb": "^3.0.0"
}
Expand Down

0 comments on commit 76095fd

Please sign in to comment.