更新公会的前缀不需要完全重新启动才能应用,但出于某种原因将新公会添加到我的数据库中确实如此
Posted
技术标签:
【中文标题】更新公会的前缀不需要完全重新启动才能应用,但出于某种原因将新公会添加到我的数据库中确实如此【英文标题】:Updating the prefix for a guild doesent require a full restart to apply but adding a new guild to my database does for some reason 【发布时间】:2021-09-26 12:13:48 【问题描述】:所以我有代码设置,所以当公会加入时,它们会以我设置的默认设置添加到数据库中。如果他们决定更改前缀它可以正常工作,他们可以更改前缀并且它会更新并且他们可以立即开始使用它。
但由于某种原因,当添加公会时,我必须重新启动机器人或使用 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); );
【问题讨论】:
【参考方案1】:我确保我的数据库中的paidRole
变量可以设置为null
。
在创建公会时,公会会被标记为null
片刻以将其放入数据库中。我使用UPDATE
根据他们在我的 Discord 中的角色将角色更新为数据库中的1
或0
。
当您使用 UPDATE
时,它会“刷新”机器人并自动应用一些更改而不使其重新启动可能会导致一些问题。
这是我最终使用的代码:
/// 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);
try
/// insert serverid and serverownerid into servers db
await connection.query(
`INSERT INTO Servers (serverId, serverOwnerId, paidRole) VALUES ('$guild.id', '$guild.ownerID', NULL)`
);
/// insert server id into serverconfig db
await connection.query(
`INSERT INTO ServerConfig (serverId) VALUES ('$guild.id')`
);
catch(err)
console.log(err);
// 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);
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`);
await connection.query(
`UPDATE Servers SET paidRole = '1' WHERE serverId = $guild.id`
);
else
await connection.query(
`UPDATE Servers SET paidRole = '0' WHERE serverId = $guild.id`
);
);
【讨论】:
以上是关于更新公会的前缀不需要完全重新启动才能应用,但出于某种原因将新公会添加到我的数据库中确实如此的主要内容,如果未能解决你的问题,请参考以下文章