如何在 Visual Studio Code 上定义公会?Discord.js
2020-05-07
706
const Discord = require('discord.js');
const BOTCLIENT = new Discord.Client();
const Token = '...2vXOGsiI';
const Prefix = '-';
BOTCLIENT.on('ready', () => {
BOTCLIENT.user.setActivity("Prefix = '-', Made by Zachary#9293");
});
BOTCLIENT.on('ready', () =>{
console.log('online');
})
BOTCLIENT.on('message', message=>{
let args = message.content.substring(Prefix.length).split(" ");
switch(args[0]){
case 'Commands':
message.channel.send("My commands are here: 'https://docs.google.com/document/d/...aBbo6k0/edit?usp=sharing'" )
break;
case 'Invite':
message.channel.send("My invite is 'https://discordapp.com/oauth2/authorize?client_id=...382&scope=BOT%20BOIIIIIIIIII&permissions=8'")
break;
case 'CreateChannel':
guildMember.roles.add(['123456789012345678', '098765432109876543']);
break;
}
})
BOTCLIENT.login(Token);
是我的代码,但我收到错误“定义公会”我在谷歌上搜索了一次又一次,但找不到如何定义公会!请有人帮帮我。
这是我收到的错误:
ReferenceError: guild is not defined
at Client.<anonymous> (c:\Users\OKBOO\OneDrive\Desktop\Discord Bot\index.js:34:9)
at Client.emit (events.js:327:22)
at MessageCreateAction.handle (c:\Users\OKBOO\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (c:\Users\OKBOO\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (c:\Users\OKBOO\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
at WebSocketShard.onPacket (c:\Users\OKBOO\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
at WebSocketShard.onMessage (c:\Users\OKBOO\OneDrive\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
at WebSocket.onMessage (c:\Users\OKBOO\OneDrive\Desktop\Discord Bot\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (c:\Users\OKBOO\OneDrive\Desktop\Discord Bot\node_modules\ws\lib\websocket.js:800:20)
2个回答
通过查看此代码,我怀疑它与以下内容有关:
guildMember.roles.add(['123456789012345678', '098765432109876543']);
我没有看到 guildmember 在任何地方得到定义。
您可以尝试使用
let guildMember = message.member
您可以使用
let guild = message.guild
此外,正如 Syntle 所说,您不需要两个就绪事件,只需将它们都放入一个事件中即可
BOTCLIENT.on('ready', () => {
BOTCLIENT.user.setActivity("Prefix = '-', Made by Zachary#9293");
console.log('online');
});
至于将您的令牌存储在不同的文件中,您可以这样做:
// index.js
const config = require("./config.js")
const token = config.token
// config.js
module.exports = {
token: "REALTOK3N"
}
Supesu
2020-05-07
是的,Unkwn 是对的,
guildMember
未定义。
如果您尝试为发送命令的人添加角色,请将其替换为
message.member
整个 ine 将是:
message.member.roles.add(['123456789012345678', '098765432109876543']);
BadSpencer
2020-05-07