我计划用机器人创建一个不一致的服务器。有很多(总共6个),并且只是具有某些背景故事的虚构人物。我很新,所有这些都太复杂了,我无法自己编写代码,因此请您帮忙!我只想为我的朋友们提供一台不错的服务器,并让我拥有一些令人愉悦的机器人,而所有这些拼命地尝试获取一些有用代码的时间都让我发疯。.
我只能使用前缀“-”来使一个机器人来做事情。 它可以更改状态(观看,收听,播放)和他正在做的事情的名称。 我不太确定为什么流式传输不起作用,或者总体上说这是否可行,但是如果可以,那真的很酷。
我的状态码:(第一个问题)
client.once('ready', () => {
console.log('Bot is ready!');
if (config.activity.streaming == true) {
client.user.setActivity(config.activity.game, {type: 'WATCHING'}); //STREAMING, PLAYING, LISTENING
} else {
client.user.setActivity(config.activity.game, {url: 'https://twitch.tv/usrname'});
client.user.setStatus('idle'); //dnd, idle, online, invisible
}
});
config.json
"activity": {
"streaming": true,
"game": "Whatevergame"
}
}
正如我所说,由于某些原因,流媒体传输不起作用,并且状态(空闲,dnd ..)也不起作用。
第二个问题
如果我尝试使用登录名添加其他机器人,它将同时登录两个机器人,但是其中只有一个机器人可以工作,这实际上是很合逻辑的,因为所有命令仅针对一个机器人进行。因此,我试图找出如何将它们全部打包到主文件中。
第三个问题
我使用了try-catch函数来执行我预先设置的命令,如果没有命令,它将发送一条错误消息。亲自看看:
client.on('message', message =>{
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
try {
client.commands.get(command).execute(message, args);
}
catch {
message.channel.send("I don't know that, sorry.");
}
});
因此,每当我键入另一个命令(我不不希望机器人进行响应)时,它都会以“我不知道[...]”进行响应,这足以只需为“其他命令”设置另一个前缀即可解决该问题,因此漫游器知道对于以ae“-”开头的每个前缀,如果该命令不存在,它必须发送一条错误消息。但是对于其他前缀(例如“?”),它应该执行其他命令。
第四个问题 我(当前)的最后一个问题是欢迎信息。我的代码:
index.js
const welcome = require("./welcome");
welcome (client)
welcome.js
module.exports = (client) => {
const channelId = '766761508427530250' // welcome channel
const targetChannelId = '766731745960919052' //rules and info
client.on('guildMemberAdd', (member) => {
console.log(member)
const message = `New user <@${member.id}> joined the server. Please read through ${member.guild.channels.cache.get(targetChannelId).toString()} to gain full access to the server!`
const channel = member.guild.channels.cache.get(channelId)
channel.send(message)
})
}
代码工作得非常好,但是增加一些多样性会更加令人兴奋。我正在尝试获取由机器人随机选择的多个欢迎消息。.我考虑将Mathfloor作为一种方法,但我不太确定该方法如何工作。.
感谢您阅读我的文字,希望我很快能和我的同伴们一起享用服务器!
干杯!
答案 0 :(得分:0)
我不确定ClientUser.setActivity()
和ClientUser.setStatus
为什么不起作用。在STREAMING
示例中,可能是因为您未指定活动的type
。无论哪种方式,都有一种更简单的方式来完成您的工作,即ClientUser.setPresence()
。这种方法有点像其他两种方法的组合。
client.once('ready', () => {
console.log('Bot is ready!');
config.activity.streaming
? client.user.setPresence({
activity: { name: config.activity.game, type: 'WATCHING' },
})
: client.user.setPresence({
activity: {
name: config.activity.game,
type: 'STREAMING',
url: 'https://twitch.tv/usrname',
},
status: 'idle', // note: the idle icon is overwritten by the STREAMING icon, so this won't do much
});
});
要在一个文件中制作多个彼此完全相同的机器人非常困难。我建议仅使用大量Array.prototype.forEach()
循环将所有事件等都应用于两个客户端。
[1, 2, 3].forEach((num) =>
console.log(`The element I'm currently iterating a function through is ${num}`)
);
// example main file
const { Client, Collection } = require('discord.js');
const [roseClient, sunflowerClient] = [new Client(), new Client()];
// adding properties:
[roseClient, sunflowerClient].forEach((client) => client.commands = new Collection())
// events
[roseClient, sunflowerClient].forEach((client) =>
client.on('message', (message) => {
// ...
});
);
// login
roseClient.login('token1');
sunflowerClient.login('token2');
同样,forEach()
循环保存了一天(❁´◡`❁)。但是,这次您实际上应该使用Array.prototype.every()
,如果数组的每个元素都通过给定的测试,则它将返回true。
基本上,如果我们使用普通的forEach()
循环,则即使其中一个前缀找到了匹配项,另一个也不会,并且错误消息将始终发送出去。因此,相反,我们将使用every()
仅在双方前缀均未找到匹配项时才发送错误消息。
// what if we ony wanted the error message if *every* number was 3
[1, 2, 3].forEach((num) => {
if (num === 3) console.error('Error message');
});
console.log('--------------------');
// now it will only send if all numbers were three (they're not)
if ([1, 2, 3].every((num) => num === 3))
console.error('Error message');
client.on('message', (message) => {
if (['-', '?'].every((prefix) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
try {
// it did not pass the test (the test being finding no match), and the error message should not be displayed
return false;
client.commands.get(command).execute(message, args);
} catch {
// it did pass the test (by finding no match). if the next test is failed too, the error message should be displayed
return true;
message.channel.send("I don't know that, sorry.");
}
});
});
您在正确的轨道上! Math.floor()
绝对是从数组中获取随机元素的正确方法。
function chooseFood() {
// make an array
const foods = ['eggs', 'waffles', 'cereal', "nothing (●'◡'●)", 'yogurt'];
// produce a random integer from 0 to the length of the array
const index = Math.floor(Math.random() * foods.length);
// find the food at that index
console.log(`Today I will eat ${foods[index]}`);
};
<button onClick="chooseFood()">Choose What to Eat</button>
module.exports = (client) => {
client.on('guildMemberAdd', (member) => {
console.log(member);
const welcomes = [
`Welcome ${member}!`,
`Woah! Didn't see you there ${member}; welcome to the server!`,
`${member} enjoy your stay at ${member.guild}!`,
];
const message = `${
welcomes[Math.floor(Math.random() * welcomes.length)]
} Please read through ${member.guild.channels.cache.get(
'766731745960919052'
)} to gain full access to the server!`;
member.guild.channels.cache.get('766761508427530250').send(message);
});
};
那是一口(╯°□°)╯︵┻━┻