我正在研究一个高级的不和谐机器人。通过cron作业可以使用一些功能。我正在开发的一个新功能要求机器人收集对带有特定表情符号的特定消息做出反应的用户。基于此,用户将获得一个临时角色(该角色很可能拥有相同类型的问题,因为它的文档与当前问题相同)。
我有提取特定消息所需的所有数据,可能还有它的内容,但我不知道如何通过cron访问它。
我有不和谐服务器ID,不和谐频道ID,不和谐消息ID和雪花表情。所以理论上我应该可以通过API访问它的所有内容。但是,我注意到大多数功能都是通过缓存的数据实现的,而且只有在事件发生时才实现。
它所做的一个类似的cron工作是将每天的概览发送到启用它的特定通道。client.channels.get([通道id]).send([格式化数据]);
然而,有了这个功能,我需要得到一条消息,或者是所有对雪花表情做出反应的用户的集合(理想情况下)。
它必须通过cron,使用discord命令,因为自动化是关键,所以这是不可行的。它还应该在没有任何缓存数据的情况下工作(真正的问题所在)。再一次,我有所有的数据需要拉消息和找到基于雪花的emoji,我只是不知道如何做通过cron。bot还对其操作的服务器具有所有所需的权限。
如有任何建议或解决方案,将不胜感激。
cronjob中的当前代码:
(async () => {
// walk through channels in DB
var channels = await query([query]);
for (var i = 0; i < channels.length; i++) {
var serv_channel_id = channels[i].server_channel_id;
var disc_channel_id = channels[i].discord_channel_id;
var disc_server_id = channels[i].discord_server_id;
// walk through messages in DB
var messages = await query([query]);
for (var j = 0; j < messages.length; j++) {
var serv_message_id = messages[i].server_message_id;
var disc_message_id = messages[i].discord_message_id;
// walk through reactions in DB
var reactions = await query([query]);
for (var k = 0; k < reactions.legnth; k++) {
var serv_reaction_id = reactions[i].server_react_id;
var r_to_give = reactions[i].role_to_give;
// get discord list of users that reacted with emoji
// check per user if they have the role, if they dont then assign role
}
}
}
})();
工作目前比效率更重要。我没有提出这些问题,原因是显而易见的,而这些问题并不是这项质询所必需的资料。
通过原始事件,我能够解决这个问题。
我的旧代码已被完全删除,因此可以忽略作为参考资料。
相反,我深入研究了API的一个我以前没有见过的领域;原始事件。
如果您想了解更多信息,我强烈建议您从这里开始阅读:https://anidiots.guide/coding-guides/raw-events
为了达到我想要的目的而使用的基本代码
client.on('raw', packet => {
(async () => {
// We don't want this to run on unrelated packets
if (!['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)) return;
// Grab the channel to check the message from
const channel = client.channels.get(packet.d.channel_id);
// make sure channel id is in DB otherwise no need to listen
const check_channel = await query([query]);
if (!Array.isArray(check_channel) || !check_channel.length) return;
// make sure the message is in the DB otherwise no need to listen
const check_message = await query([query]);
if (!Array.isArray(check_message) || !check_message.length) return;
channel.fetchMessage(packet.d.message_id).then(message => {
(async () => {
// Emojis can have identifiers of name:id format, so we have to account for that case as well
const emoji = packet.d.emoji.id ? `${packet.d.emoji.name}:${packet.d.emoji.id}` : packet.d.emoji.name;
// make sure emoji id is in db
const check_reaction = await query([query]);
if (!Array.isArray(check_reaction) || !check_reaction.length) return;
// add and track user
if (packet.t === 'MESSAGE_REACTION_ADD') {
await query([query]);
}
// remove user from tracking
if (packet.t === 'MESSAGE_REACTION_REMOVE') {
await query([query]);
}
})();
});
})();
});
这段代码并不完美,也不干净,但对于那些陷入类似问题的人来说,它是一个基本的开始。对于我的特殊问题,这就是解决方案。