javascript - Discord Bot 登录超过 1000 次
问题描述
我怀疑这是由于下面的代码而发生的。它是唯一具有此代码的 3 机器人。当机器人登录时,代码本身并没有 100% 工作。它应该给任何正在直播的人一个“流媒体”角色。有些人它添加了角色,而对于其他人则没有。
如果可能的话,我想为这两个问题提供帮助。主要是日志记录问题,因为我什至无法让机器人保持在线状态
下面的代码显示了整个 index.js 文件。上面谈到的代码是代码的“presenceUpdate”部分。
const fs = require('fs');
const Discord = require('discord.js');
const {prefix, token} = require('./config.json');
const welcomeGif = require('./welcomeGifs.json');
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log(`${client.user.username} is online!`);
});
client.on('guildMemberAdd', gif => {
gif = new Discord.Attachment(welcomeGif[Math.floor(Math.random() * welcomeGif.length)]);
client.channels.get('614134721533968494').send(gif);
});
client.on('presenceUpdate', (oldPresence, newPresence) => {
const guild = newPresence.guild;
const streamingRole = guild.roles.cache.find(role => role.id === '720050658149138444');
if (newPresence.user.bot || newPresence.presence.clientStatus === 'mobile' || oldPresence.presence.status !== newPresence.presence.status) return;
const oldGame = oldPresence.presence.activities ? oldPresence.presence.activities.streaming: false;
const newGame = newPresence.presence.activities ? newPresence.presence.activities.streaming: false;
if (!oldGame && newGame) { // Started playing.
newPresence.roles.add(streamingRole)
.then(() => console.log(`${streamingRole.name} added to ${newPresence.user.tag}.`))
.catch(console.error);
} else if (oldGame && !newGame) { // Stopped playing.
newPresence.roles.remove(streamingRole)
.then(() => console.log(`${streamingRole.name} removed from ${newPresence.user.tag}.`))
.catch(console.error);
}
});
// This is the start of the main function when the bot is turned on
client.on('message', message => {
// The bot will not respond if there is no prefix,
// the user that typed it was a bot,
// or if it was not sent from in the server
if (!message.content.startsWith(prefix) || message.author.bot || !message.guild) return;
// Creates the arguments variable and separates it with a space
// and creates the command variable
const args = message.content.slice(prefix.length).split(' ');
const commandName = args.shift().toLowerCase();
if (!client.commands.has(commandName)) return;
const command = client.commands.get(commandName);
if (command.guildOnly && message.channel.type !== 'text') {
return message.reply('I can\'t execute that command inside DMs!');
}
try {
command.execute(message, args);
}
catch (error) {
console.error(error);
message.channel.send('There was an error trying to execute that command!\nCheck the console for details.');
}
});
// This logs in the bot with the specified token found in config
client.login(token);
解决方案
好吧,我想我越来越近了。我认为问题在于机器人登录后,它会因我发现的错误而崩溃。当它崩溃时,它被设置为自动重启,所以当它重新启动时它再次崩溃并重复循环。我发现的错误是存在更新部分中的“clientStatus”未定义。
推荐阅读
- python - 我如何处理玩家生命?
- node.js - 在 Sequelize 的多对多关联中包含中间表
- php - 如何根据我的代码将复选框数据保存到数据库中,知道吗?
- c# - DragAndDrop 无法使用 C# 更改 gridview 行的顺序
- google-apps-script - Google工作表的电子邮件功能中未定义的错误
- visual-c++ - 重置一堆单选按钮
- c# - .NET Core 2.2 检查数据库是否存在
- javascript - 如何在 npm 包中为 Angular 项目创建组件
- python - 为什么我的由 boost.python 和 c++ 头文件编译的 .so 文件失败了?
- android - 约束边距在约束布局上不能正常工作?