我正在为赠品发出索赔命令,以便机器人显示用户拥有的角色的索赔时间
I was making a claim command for giveaways so that the bot shows the claim time for the role which the user has
我想知道这段代码中的错误以及解决方法。提前致谢!
当赠品机器人宣布获胜者时,它以 Congratulations <@user> 开头。 . . 机器人需要检查@user 的角色并让主持人知道获胜者的领取时间。如果我能尽早得到回复,我将不胜感激。我正在使用 repl.it 对机器人进行编码(它是私有的,仅适用于我的服务器)并且它在行中显示错误,
if (message.member.roles.has(allowedRole.id) {
我想要一个解决方案来解决我的问题。
let winner = msg.mentions.users.first();
if (!winner) msg.reply("Unexpected Error");
let allowedRole = message.guild.roles.find("name", "・supporter");
if (message.member.roles.has(allowedRole.id) {
msg.channel.send(`GG! You have **15** seconds to DM the host!`)
.then(message => { setTimeout(function() { message.edit('**Time up!**')) }, 15000)})
} else {
let winner = msg.mentions.users.first();
if (!winner) msg.reply("Unexpected Error");
let allowedRole = message.guild.roles.find("name", "・donator");
if (message.member.roles.has(allowedRole.id) {
msg.channel.send(`GG! You have **25** seconds to DM the host!`)
.then(message => { setTimeout(function() { message.edit('**Time up!**')) }, 25000)})
} else {
let winner = msg.mentions.users.first();
if (!winner) msg.reply("Unexpected Error");
let allowedRole = message.guild.roles.find("name", "・booster");
if (message.member.roles.has(allowedRole.id) {
msg.channel.send(`GG! You have infinite time to DM!`)})
}
}
我发现您的代码存在一些问题。
首先,您不断地在 msg
和 message
之间切换。我将假设这些不是您要访问的两个独立对象,因此您应该只使用其中之一。
其次,您没有正确搜索角色。自 discord.js v12 以来,您必须在从集合中请求数据时使用 cache
(有关 v12 中更改内容的更多信息,请参阅 here)。另外,据我所知,在搜索角色时,您不能使用 ("name", "<name>")
。在您的情况下,您需要使用 msg.member.roles.cache.has(allowedRole.id)
和 msg.guild.roles.cache.find(r => r.name === "・booster")
第三,您检查的是消息作者的角色,而不是获胜者。我假设您想检查获胜者是否具有这些特定角色,而不是选择获胜者的用户。
最后,您在将括号打开时遇到了很多问题。这也会导致大量错误。
我清理了你的代码。这对你来说应该会更好
const winner = msg.mentions.members.first(); //Get the winner
if (!winner) { //Send an error if there's no winner specified
return msg.reply("Unexpected Error");
};
var time = 0; //This is blank because we'll edit it later
//The line below will look through the winner's roles, find either of the three specified roles, and return the first one it finds. It will look for booster first, followed by donator, then supporter
const allowedRole = winner.roles.cache.find(r => r.name === "・booster") || winner.roles.cache.find(r => r.name === "・donator") || winner.roles.cache.find(r => r.name === "・supporter");
if (!allowedRole) { //Do something if the winner doesn't have one of the needed roles
return;
};
switch (allowedRole.name) { //Set the time appropriately depending on the role name
case "・supporter":
time = 1500;
break;
case "・donator":
time = 2500;
break;
case "・booster":
time = "infinite";
break;
};
if (time === "infinite") { //Send one message if they have infinite time, and another if not
msg.channel.send(`GG! You have infinite time to DM!`);
} else {
msg.channel.send(`GG! You have **${time / 1000}** seconds to DM the host!`) //Divide the time by one thousand since we stored it in milliseconds
.then(message => {
setTimeout(function () {
message.edit('**Time up!**');
}, time); //Set the timeout to use our specified time
});
};
通常在编码时您希望尽可能避免重复自己。当您只需要大部分代码一次时,您实际上已经拥有了三次完全相同的代码。此代码无需重复您自己。
我想知道这段代码中的错误以及解决方法。提前致谢! 当赠品机器人宣布获胜者时,它以 Congratulations <@user> 开头。 . . 机器人需要检查@user 的角色并让主持人知道获胜者的领取时间。如果我能尽早得到回复,我将不胜感激。我正在使用 repl.it 对机器人进行编码(它是私有的,仅适用于我的服务器)并且它在行中显示错误,
if (message.member.roles.has(allowedRole.id) {
我想要一个解决方案来解决我的问题。
let winner = msg.mentions.users.first();
if (!winner) msg.reply("Unexpected Error");
let allowedRole = message.guild.roles.find("name", "・supporter");
if (message.member.roles.has(allowedRole.id) {
msg.channel.send(`GG! You have **15** seconds to DM the host!`)
.then(message => { setTimeout(function() { message.edit('**Time up!**')) }, 15000)})
} else {
let winner = msg.mentions.users.first();
if (!winner) msg.reply("Unexpected Error");
let allowedRole = message.guild.roles.find("name", "・donator");
if (message.member.roles.has(allowedRole.id) {
msg.channel.send(`GG! You have **25** seconds to DM the host!`)
.then(message => { setTimeout(function() { message.edit('**Time up!**')) }, 25000)})
} else {
let winner = msg.mentions.users.first();
if (!winner) msg.reply("Unexpected Error");
let allowedRole = message.guild.roles.find("name", "・booster");
if (message.member.roles.has(allowedRole.id) {
msg.channel.send(`GG! You have infinite time to DM!`)})
}
}
我发现您的代码存在一些问题。
首先,您不断地在 msg
和 message
之间切换。我将假设这些不是您要访问的两个独立对象,因此您应该只使用其中之一。
其次,您没有正确搜索角色。自 discord.js v12 以来,您必须在从集合中请求数据时使用 cache
(有关 v12 中更改内容的更多信息,请参阅 here)。另外,据我所知,在搜索角色时,您不能使用 ("name", "<name>")
。在您的情况下,您需要使用 msg.member.roles.cache.has(allowedRole.id)
和 msg.guild.roles.cache.find(r => r.name === "・booster")
第三,您检查的是消息作者的角色,而不是获胜者。我假设您想检查获胜者是否具有这些特定角色,而不是选择获胜者的用户。
最后,您在将括号打开时遇到了很多问题。这也会导致大量错误。
我清理了你的代码。这对你来说应该会更好
const winner = msg.mentions.members.first(); //Get the winner
if (!winner) { //Send an error if there's no winner specified
return msg.reply("Unexpected Error");
};
var time = 0; //This is blank because we'll edit it later
//The line below will look through the winner's roles, find either of the three specified roles, and return the first one it finds. It will look for booster first, followed by donator, then supporter
const allowedRole = winner.roles.cache.find(r => r.name === "・booster") || winner.roles.cache.find(r => r.name === "・donator") || winner.roles.cache.find(r => r.name === "・supporter");
if (!allowedRole) { //Do something if the winner doesn't have one of the needed roles
return;
};
switch (allowedRole.name) { //Set the time appropriately depending on the role name
case "・supporter":
time = 1500;
break;
case "・donator":
time = 2500;
break;
case "・booster":
time = "infinite";
break;
};
if (time === "infinite") { //Send one message if they have infinite time, and another if not
msg.channel.send(`GG! You have infinite time to DM!`);
} else {
msg.channel.send(`GG! You have **${time / 1000}** seconds to DM the host!`) //Divide the time by one thousand since we stored it in milliseconds
.then(message => {
setTimeout(function () {
message.edit('**Time up!**');
}, time); //Set the timeout to use our specified time
});
};
通常在编码时您希望尽可能避免重复自己。当您只需要大部分代码一次时,您实际上已经拥有了三次完全相同的代码。此代码无需重复您自己。