我正在尝试编写自己的不和谐机器人,现在我想发出免费命令... 我研究了一个示例代码并找到了它。我测试了它,但是它不起作用...
const ms = require('ms');
const chalk = require('chalk');
const consolewords = chalk.keyword('white');
const consoleerrcmds = chalk.keyword('yellow');
module.exports = {
name: 'gcreate',
description: 'Start a giveaway',
cooldown: 5,
args: true,
guildOnly: true,
usage: '|Channel| [Duration] (Winner) {Prize}',
permission: 'Operator',
id: 115,
execute: async (client, message, args) => {
if (message.member.hasPermission(['SEND_TTS_MESSAGES'])) {
const giveawayChannel = message.mentions.channels.first();
if(!giveawayChannel) {
return message.channel.send(':x: You have to mention a valid channel!');
}
const giveawayDuration = args[1];
if(!giveawayDuration || isNaN(ms(giveawayDuration))) {
return message.channel.send(':x: You have to specify a valid duration!');
}
const giveawayNumberWinners = args[2];
if(isNaN(giveawayNumberWinners) || (parseInt(giveawayNumberWinners) <= 0)) {
return message.channel.send(':x: You have to specify a valid number of winners!');
}
const giveawayPrize = args.slice(3).join(' ');
if(!giveawayPrize) {
return message.channel.send(':x: You have to specify a valid prize!');
}
client.giveawaysManager.start(giveawayChannel, {
time: ms(giveawayDuration),
prize: giveawayPrize,
winnerCount: giveawayNumberWinners,
hostedBy: client.config.hostedBy ? message.author : null,
messages: {
giveaway: (client.config.everyoneMention ? '@everyone\n\n' : '') + '?? **GIVEAWAY** ??',
giveawayEnded: (client.config.everyoneMention ? '@everyone\n\n' : '') + '?? **GIVEAWAY ENDED** ??',
timeRemaining: 'Time remaining: **{duration}**!',
inviteToParticipate: 'React with ? to participate!',
winMessage: 'Congratulations, {winners}! You won **{prize}**!',
embedFooter: 'Giveaways',
noWinner: 'Giveaway cancelled, no valid participations.',
hostedBy: 'Hosted by: {user}',
winners: 'winner(s)',
endedAt: 'Ended at',
units: {
seconds: 'seconds',
minutes: 'minutes',
hours: 'hours',
days: 'days',
pluralS: false,
},
},
});
message.channel.send(`Giveaway started in ${giveawayChannel}!`);
}
if (!message.member.hasPermission(['SEND_TTS_MESSAGES'])) {
message.channel.send('You don\'t have the permission to do that!');
console.log(consolewords('[CMD]: ') + consoleerrcmds(`${message.author.username} wanted to create a giveaway but didn't have the permission!`));
}
},
};
我收到此错误代码:UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'hasPermission' of undefined
。
我不确定是什么问题,因为相同的if语句可在其他命令上使用!
提前感谢您的帮助!
Ps:我安装了所有软件包。因此应该没有问题!
答案 0 :(得分:0)
请尝试使用此方法(如果愿意,请确保为您的令牌创建.env文件):
const Discord = require('discord.js')
const { Client, Attachment, MessageEmbed } = ('discord.js');
const bot = new Discord.Client();
const ms = require("ms");
require('events').EventEmitter.defaultMaxListeners = 60;
const token = process.env.DISCORD_TOKEN;
const PREFIX = '!';
const { GiveawaysManager } = require("discord-giveaways");
const manager = new GiveawaysManager(bot, {
storage: "./giveaways.json",
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: "#FF0000",
reaction: "?"
}
})
bot.on('ready', () => {
console.log(`${bot.user.tag} is ready to go.`)
})
bot.giveawaysManager = manager;
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return;
let args = message.content.substring(PREFIX.length).split(" ");
if (message.author.bot) return;
let msg = message.content.toLowerCase();
if (message.channel.type === 'dm') {
return;
}
switch (args[0]) {
case 'gcreate':
if (!message.member.hasPermission("MANAGE_MESSAGES")) return message.channel.send('You do not have permission to use this command')
let channel = message.mentions.channels.first();
if (!channel) return message.channel.send('Please provide a channel | Usage: !gcreate {channel} {duration} {winners} {prize} ex: !gcreate #giveaways 1d 1 Awsome Prize');
let giveawayDuration = args[2];
if (!giveawayDuration || isNaN(ms(giveawayDuration))) return message.channel.send('Pleae provide a valid duration | Usage: !gcreate {channel} {duration} {winners} {prize} ex: !gcreate #giveaways 1d 1 Awsome Prize');
let giveawayWinners = args[3];
if (isNaN(giveawayWinners) || (parseInt(giveawayWinners) <= 0)) return message.channel.send('Please provide a valid number of winners! | Usage: !gcreate {channel} {duration} {winners} {prize} ex: !gcreate #giveaways 1d 1 Awsome Prize');
let giveawayPrize = args.slice(4).join(" ");
if (!giveawayPrize) return message.channel.send('What prize do you wanna give? | Usage: !gcreate {channel} {duration} {winners} {prize} ex: !gcreate #giveaways 1d 1 Awsome Prize');
bot.giveawaysManager.start(channel, {
time: ms(giveawayDuration),
prize: giveawayPrize,
winnerCount: giveawayWinners,
hostedBy: message.author,
messages: {
giveaway: "??**GIVEAWAY**??",
giveawayEnded: "??**GIVEAWAY ENDED**??",
timeRemaining: "Time remaining: **{duration}**",
inviteToParticipate: "React with ? to enter",
winMessage: "Congrats {winners}, you won **{prize}**",
embedFooter: "Giveaway time!",
noWinner: "Couldn't determine a winner",
hostedBy: "Hosted by {user}",
winners: "winner(s)",
endedAt: "Ends at",
units: {
seconds: "seconds",
minutes: "minutes",
hours: "hours",
days: "days",
pluralS: false
}
}
});
message.channel.send(`Giveaway starting in ${channel}`);
break;
}
});
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return;
let args = message.content.substring(PREFIX.length).split(' ');
if (message.author.bot) return;
if (message.channel.type === 'dm') {
return;
}
switch (args[0]) {
case 'gedit':
if (!message.member.hasPermission("ADMINISTRATOR")) return message.channel.send('You do not have permission to use this command')
let messageID = args[1];
let giveawayDuration = args[2];
if (!giveawayDuration || isNaN(ms(giveawayDuration))) return message.channel.send('Pleae provide a valid duration | ex: !gedit {messageid} 30s 3 nitro');
let giveawayWinners = args[3];
if (isNaN(giveawayWinners) || (parseInt(giveawayWinners) <= 0)) return message.channel.send('Please provide a valid number of winners! | ex: !gedit {messageid} 30s 3 nitro')
let giveawayPrize = args.slice(4).join(" ");
if (!messageID) message.channel.send('Please provide a message ID.')
bot.giveawaysManager.edit(messageID, {
newWinnerCount: giveawayWinners,
newPrize: giveawayPrize,
addTime: ms(giveawayDuration)
}).then(() => {
message.channel.send("Success! Giveaway will updated very soon.");
}).catch((_err) => {
message.channel.send("No giveaway found for " + messageID + ", please check and try again");
});
break;
};
});
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return;
let args = message.content.substring(PREFIX.length).split(' ');
if (message.author.bot) return;
if (message.channel.type === 'dm') {
return;
}
switch (args[0]) {
case 'gdelete':
if (!message.member.hasPermission("ADMINISTRATOR")) return message.channel.send('You do not have permission to use this command')
let messageID = args[1];
if (!messageID) return message.channel.send('No message ID provided')
bot.giveawaysManager.delete(messageID).then(() => {
message.channel.send("Success! Giveaway deleted!");
}).catch((_err) => {
message.channel.send("No giveaway found for " + messageID + ", please check and try again");
});
break;
};
});
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return;
let args = message.content.substring(PREFIX.length).split(' ');
if (message.author.bot) return;
if (message.channel.type === 'dm') {
return;
}
switch (args[0]) {
case 'greroll':
if (!message.member.hasPermission("ADMINISTRATOR")) return message.channel.send('You do not have permission to use this command')
let messageID = args[1];
if (!messageID) return message.channel.send('No message ID provided')
bot.giveawaysManager.reroll(messageID).then(() => {
message.channel.send("Success! Giveaway rerolled!");
}).catch((_err) => {
message.channel.send("No giveaway found for " + messageID + ", please check and try again");
});
break;
};
});
bot.login(token)