所以我有代码设置,所以当公会加入时,它们会以我设置的默认设置添加到数据库中。如果他们决定更改前缀它可以正常工作,他们可以更改前缀并且它会更新并且他们可以立即开始使用它。
但是由于某种原因,当添加公会时,我必须重新启动机器人或使用 nodemon 并使其在该新公会发生更改时自动重启,然后才能使用任何命令。与添加前缀相比,我将此信息添加到数据库的方式有什么不同吗?
下面是我用来将服务器添加到数据库的代码,下面是我用来让它们更改前缀的代码。我试图让命令为刚刚邀请机器人的服务器工作,而不必重新启动机器人,以避免在尝试做某事然后重新启动时出现错误,因为有人邀请了机器人。
/// adding a guild to the database upon invite
bot.on('guildCreate', async (guild) => {
// Guild the user needs to have the role in
let myGuild = await bot.guilds.fetch(process.env.BOT_GUILD);
console.log(myGuild);
// Role that the user needs
let requiredRole = process.env.PAID_ROLE;
console.log(requiredRole);
// find default channel
let defaultChannel = "";
guild.channels.cache.forEach((channel) => {
if(channel.type == "text" && defaultChannel == "") {
if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
defaultChannel = channel;
console.log(defaultChannel);
}
}
});
// Member object of the user in guildA
try{
let guildOwner = await myGuild.members.fetch(guild.ownerID);
console.log(guildOwner);
if (!guildOwner)
return console.log(`Oops, ${guild.owner} is not a member of your server.`);
}catch(error) {
return console.log(`Oops, ${guild.owner} is not a member of your server.`),
defaultChannel.send('Please kick the bot, have the guild owner join this discord https://discord.gg/tDTjBAaCAn, Then you can reinvite the bot and you will be properly added to the database and can use the bot! dont forget to check out the premium features while your there if you decide you want more features from Gate Bot!');
}
//Check if they have the role
let guildOwner = await myGuild.members.fetch(guild.ownerID);
let ownerHasPaidRole = guildOwner.roles.cache.has(process.env.PAID_ROLE);
if (ownerHasPaidRole){
console.log(`Woohoo, ${guildOwner} has the required role`);}
try {
/// insert serverid and serverownerid into servers db
await connection.query(
`INSERT INTO Servers (serverId, serverOwnerId, paidRole) VALUES ('${guild.id}', '${guild.ownerID}', 1)`
);
/// insert server id into serverconfig db
await connection.query(
`INSERT INTO ServerConfig (serverId) VALUES ('${guild.id}')`
);
}catch(err){
console.log(err);
}});
使用更改前缀命令的消息处理程序
///allowing the script to see files from the commands folder
bot.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
bot.commands.set(command.name, command);
}
///Message Handler
bot.on('message', async (message) => {
const prefix = guildCommandPrefixes.get(message.guild.id);
console.log('caught message');
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
///basic ping pong test command
if(command === 'help'){
bot.commands.get('help').execute(message);
}
///basic ping pong test command
else if(command === 'ping'){
bot.commands.get('ping').execute(message);
}
///change the servers prefix
else if(command === 'changeprefix'){
bot.commands.get('changeprefix').execute(message, connection, prefix, guildCommandPrefixes);
}
///arguments test
else if (command === 'argsinfo'){
bot.commands.get('argsinfo').execute(message, command, args)
}
///message command list
else if (command === 'autoungate'){
bot.commands.get('autoungate').execute(message, args, Discord);
}});
用于更改前缀的命令。
module.exports= {
name: 'changeprefix',
description: "this will change the prefix in the database for the set guild id",
execute: async (message, connection, prefix, guildCommandPrefixes) =>{
setTimeout(() => {message.delete();}, 3000);
if (message.content.toLowerCase().startsWith(prefix + 'changeprefix')){
if (message.member.id === message.guild.ownerID) {
var guild = message.guild
paidRole = await connection.query(`SELECT paidRole FROM Servers Where '${guild}' === serverId`);
const [cmdName, newPrefix ] = message.content.split(" ");
if (paidRole === '1'){
if (newPrefix) {
try {
await connection.query(
`UPDATE ServerConfig SET cmdPrefix = '${newPrefix}' WHERE serverId= '${message.guild.id}'`
);
guildCommandPrefixes.set(message.guild.id, newPrefix);
message.channel.send(`Updated guild prefix to ${newPrefix}`).then(sentMessage => {sentMessage.delete({ timeout: 3000}); });
}catch(err) {
console.log(err);
message.channel.send(`Failed to update guild prefix to ${newPrefix}`).then(sentMessage => {sentMessage.delete({ timeout: 3000}); });
}}
else {
message.channel.send('You need to input a prefix to change to!').then(sentMessage => {sentMessage.delete({ timeout: 3000}); });
}}
else {
message.channel.send('You need to purchase the premium version for prefix customization.').then(sentMessage => {sentMessage.delete({ timeout: 3000}); });
}}
else {
message.channel.send('You do not have permissions to do this command!').then(sentMessage => {sentMessage.delete({ timeout: 3000}); });
}
}
}}