fix(discord-bot): check if the member has the role

This commit is contained in:
GramingFoxTeam
2022-11-16 16:59:12 +03:00
parent 3cbebc2842
commit 9bff68c8c4
8 changed files with 90 additions and 52 deletions

View File

@@ -1,11 +1,12 @@
{ {
"discord": { "discord": {
"token": "YOUR-BOT-TOKEN-HERE", "token": "YOUR-BOT-TOKEN-HERE",
"id": "1038762591805247518" "id": "1038762591805247518",
"trainRole": "965267139902705744"
}, },
"telegram": { "telegram": {
"token": "YOUR-BOT-TOKEN-HERE" "token": "YOUR-BOT-TOKEN-HERE"
}, },
"server": { "server": {

View File

@@ -5,11 +5,17 @@ import {
SelectMenuBuilder, SelectMenuBuilder,
ComponentType ComponentType
} from 'discord.js'; } from 'discord.js';
export default { export default {
data: new ContextMenuCommandBuilder() data: new ContextMenuCommandBuilder()
.setName('Train Message') .setName('Train Message')
.setType(ApplicationCommandType.Message), .setType(ApplicationCommandType.Message),
async execute(interaction) { async execute(interaction) {
if (!interaction.member.roles.cache.get(global.config.discord.trainRole))
return interaction.reply({
content: 'You don\'t have the permission to do this.',
ephemeral: true
});
const options = []; const options = [];
for (const { label } of global.config.responses) { for (const { label } of global.config.responses) {

View File

@@ -4,7 +4,10 @@ export default {
name: Events.MessageCreate, name: Events.MessageCreate,
once: false, once: false,
execute(msg) { execute(msg) {
if (!msg.content) return; if (!msg.content || msg.author.bot) return;
msg.client.helper.scanText(msg.content.toLowerCase(), `${msg.channelId}/${msg.id}`); msg.client.helper.scanText(
msg.content.toLowerCase(),
`${msg.channelId}/${msg.id}`
);
} }
}; };

View File

@@ -1,30 +1,40 @@
export default { export default {
command: /\/train/, command: /\/train/,
async execute(msg) { async execute(msg) {
console.log(msg); console.log(msg);
if (msg.reply_to_message.message_id === msg.message_thread_id) return global.bot.sendMessage(msg.chat.id, 'Please reply to a message!', { if (msg.reply_to_message.message_id === msg.message_thread_id)
message_thread_id: msg.message_thread_id, reply_to_message_id: msg.message_id return global.bot.sendMessage(msg.chat.id, 'Please reply to a message!', {
}); message_thread_id: msg.message_thread_id,
reply_to_message_id: msg.message_id
});
const options = []; const options = [];
for (const { label } of global.config.responses) { for (const { label } of global.config.responses) {
options.push({ options.push({
text: label, text: label,
callback_data: `label_${label.toLowerCase()}` callback_data: `label_${label.toLowerCase()}`
}); });
} }
const admins = await global.bot.getChatAdministrators(msg.chat.id); const admins = await global.bot.getChatAdministrators(msg.chat.id);
const isAdmin = admins.find(admin => admin.user.id === msg.from.id); const isAdmin = admins.find((admin) => admin.user.id === msg.from.id);
if (!isAdmin) return global.bot.sendMessage(msg.chat.id, 'You\'re not an admin.', { if (!isAdmin)
message_thread_id: msg.message_thread_id, reply_to_message_id: msg.message_id return global.bot.sendMessage(msg.chat.id, 'You\'re not an admin.', {
}); message_thread_id: msg.message_thread_id,
global.bot.sendMessage(msg.chat.id, 'Please select the corresponding label to train the bot.', { reply_to_message_id: msg.message_id
message_thread_id: msg.message_thread_id, reply_to_message_id: msg.reply_to_message.message_id, reply_markup: { });
inline_keyboard: [options] global.bot.sendMessage(
} msg.chat.id,
}); 'Please select the corresponding label to train the bot.',
} {
} message_thread_id: msg.message_thread_id,
reply_to_message_id: msg.reply_to_message.message_id,
reply_markup: {
inline_keyboard: [options]
}
}
);
}
};

View File

@@ -10,9 +10,12 @@ export default {
if (Number(aiRes.predictions[0].score) >= response.threshold) { if (Number(aiRes.predictions[0].score) >= response.threshold) {
const ids = aiRes.id.split('/'); const ids = aiRes.id.split('/');
global.bot.sendMessage(ids[0], response.text, { message_thread_id: ids[1], reply_to_message_id: ids[2] }); global.bot.sendMessage(ids[0], response.text, {
message_thread_id: ids[1],
reply_to_message_id: ids[2]
});
return; return;
} }
} }
}; };

View File

@@ -1,18 +1,30 @@
export default { export default {
name: 'callback_query', name: 'callback_query',
once: false, once: false,
async execute(cb) { async execute(cb) {
const admins = await global.bot.getChatAdministrators(cb.message.chat.id); const admins = await global.bot.getChatAdministrators(cb.message.chat.id);
const isAdmin = admins.find(admin => admin.user.id === cb.message.from.id); const isAdmin = admins.find(
(admin) => admin.user.id === cb.message.from.id
);
if (!isAdmin) return global.bot.sendMessage(cb.message.chat.id, 'You\'re not an admin.', { if (!isAdmin)
message_thread_id: cb.message.message_thread_id, reply_to_message_id: cb.message.message_id return global.bot.sendMessage(
});; cb.message.chat.id,
'You\'re not an admin.',
{
message_thread_id: cb.message.message_thread_id,
reply_to_message_id: cb.message.message_id
}
);
global.helper.sendTrainData(cb.message.reply_to_message.text.toLowerCase(), cb.data.replace('label_', '').toUpperCase()); global.helper.sendTrainData(
cb.message.reply_to_message.text.toLowerCase(),
cb.data.replace('label_', '').toUpperCase()
);
global.bot.sendMessage(cb.message.chat.id, 'Sent train data to server.', { global.bot.sendMessage(cb.message.chat.id, 'Sent train data to server.', {
message_thread_id: cb.message.message_thread_id, reply_to_message_id: cb.message.message_id message_thread_id: cb.message.message_thread_id,
}); reply_to_message_id: cb.message.message_id
} });
} }
};

View File

@@ -1,8 +1,11 @@
export default { export default {
name: 'message', name: 'message',
once: false, once: false,
async execute(msg) { async execute(msg) {
if (!msg.text) return; if (!msg.text) return;
global.helper.scanText(msg.text.toLowerCase(), `${msg.chat.id}/${msg.message_thread_id}/${msg.message_id}`); global.helper.scanText(
} msg.text.toLowerCase(),
} `${msg.chat.id}/${msg.message_thread_id}/${msg.message_id}`
);
}
};

View File

@@ -14,7 +14,7 @@ global.config = JSON.parse(configJSON);
global.helper = new HelperClient(global.config); global.helper = new HelperClient(global.config);
global.helper.connect(); global.helper.connect();
global.bot = new TelegramBot(config.telegram.token, { polling: true }); global.bot = new TelegramBot(global.config.telegram.token, { polling: true });
const commandsPath = join(__dirname, 'commands'); const commandsPath = join(__dirname, 'commands');
const commandFiles = readdirSync(commandsPath).filter((file) => const commandFiles = readdirSync(commandsPath).filter((file) =>
@@ -31,7 +31,7 @@ for (const file of commandFiles) {
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.` `[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
); );
} }
}; }
const tgEventsPath = join(__dirname, 'events/telegram'); const tgEventsPath = join(__dirname, 'events/telegram');
const tgEventFiles = readdirSync(tgEventsPath).filter((file) => const tgEventFiles = readdirSync(tgEventsPath).filter((file) =>