我试图为我的不和谐机器人创建一个斜线命令,它会自动从 e621 发送 1 张图像,例如每隔一分钟。它可以工作一两个小时。但随后抛出错误并且机器人崩溃。
这是错误:
/home/****/Desktop/discorde6Bot/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:687
throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
^
DiscordAPIError[50027]: Invalid Webhook Token
at handleErrors (/home//****//Desktop/discorde6Bot/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:687:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (/home/****/Desktop/discorde6Bot/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:1072:23)
at async SequentialHandler.queueRequest (/home/****/Desktop/discorde6Bot/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:913:14)
at async _REST.request (/home/****/Desktop/discorde6Bot/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:1218:22)
at async InteractionWebhook.send (/home/****/Desktop/discorde6Bot/node_modules/discord.js/src/structures/Webhook.js:222:15)
at async Timeout.spamLoop [as _onTimeout] (/home/****/Desktop/discorde6Bot/commands/e621spam.js:120:11) {
requestBody: {
files: [],
json: {
content: 'Something went wrong. Sorry',
tts: false,
nonce: undefined,
embeds: undefined,
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: undefined,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined,
thread_name: undefined
}
},
rawError: { message: 'Invalid Webhook Token', code: 50027 },
code: 50027,
status: 401,
method: 'POST',
url: 'https://discord.com/api/v10/webhooks/1082024195938062437/aW50ZXJhY3Rpb246MTIzNjQzODg0Njk0MjQ3ODQxNzp4QmVsa09QWUVZN0VrSkRUWHpHRVZQSzZZd2k1cUt5dzVDdENrT0JPQThkTUdlWTgza2hXdkhwalJIQWdkVHlaRkNsNlhaVjdDd2FzU0l3dmE1SHN0cURJdFdhSTFaTlAxMnFpc2JiTWsycFlRUWM4ZXVZd21XOEpyckdVSTRsdA?wait=true'
}
这是代码:
const { SlashCommandBuilder } = require("@discordjs/builders");
const axios = require('axios');
const { EmbedBuilder } = require('discord.js');
const activeSpams = new Map(); // Map to store active spams
function randomPicUrl(response, usedIndices) {
let randomIndex = Math.floor(Math.random() * response.data.posts.length);
let post;
if (usedIndices.includes(randomIndex)) {
return randomPicUrl(response, usedIndices);
} else {
post = response.data.posts[randomIndex];
usedIndices.push(randomIndex);
return post.file.url;
}
}
function containsBlacklistedTags(tags, blacklist) {
for (const tag of tags) {
let checkTagElem = tag.split(' ');
for (const e of checkTagElem) {
if (blacklist.includes(e.toLowerCase())) {
return true;
}
}
}
return false;
}
module.exports = {
data: new SlashCommandBuilder()
.setName('e621spam')
.setDescription('Start or stop sending random images from e621 with the given tags')
.addStringOption(option =>
option
.setName('action')
.setDescription('Start or stop spamming')
.setRequired(true))
.addStringOption(option =>
option
.setName('tags')
.setDescription('Tags to search for')
.setRequired(false))
.addIntegerOption(option =>
option
.setName('interval')
.setDescription('Interval between sending images (in minutes)')
.setRequired(false)),
async execute(interaction) {
const action = interaction.options.getString('action');
const tags = interaction.options.getString('tags')?.split(',') || [];
const interval = interaction.options.getInteger('interval');
const guildId = interaction.guildId;
const channelId = interaction.channelId;
const spamKey = `${guildId}-${channelId}`;
if (action === 'start') {
if (activeSpams.has(spamKey)) {
await interaction.reply({ content: "There's already an active e621spam in this channel.", ephemeral: true });
return;
}
const spamLoop = async () => {
const blacklist = ['de*****', 'go***', 'tent****', 'fa***', 'sc***', 'animated', 'type:mp4', 'type:webm'];
const e621ApiUrl = "https://e621.net/posts.json?tags=order:random " + tags;
console.log(`Constructed URL: ${e621ApiUrl}`);
try {
if (!interaction.channel.nsfw) {
await interaction.followUp({ content: "This command can only be used in NSFW channels." });
clearInterval(activeSpams.get(spamKey));
activeSpams.delete(spamKey);
return;
}
if (containsBlacklistedTags(tags, blacklist)) {
await interaction.followUp({ content: "Content contains blacklisted tags." });
clearInterval(activeSpams.get(spamKey));
activeSpams.delete(spamKey);
return;
}
const response = await axios.get(e621ApiUrl, {
headers: { "User-Agent": "Discord Bot (************ on 621)" },
maxContentLength: 8 * 1024 * 1024, // Limiting max content length to 8MB
});
console.log(response.data.posts.length);
const usedIndices = [];
const imageUrl = randomPicUrl(response, usedIndices);
if (imageUrl === null) {
return;
}
// Check file size before sending
const contentLength = response.headers['content-length'];
if (contentLength && parseInt(contentLength) > 8 * 1024 * 1024) {
console.log("File size is too big! Retrying...");
return;
}
// Create an embed message
const embed = new EmbedBuilder()
.setTitle('Random Image from e621')
.setDescription(`[View Image](${imageUrl})`)
.setImage(imageUrl)
.setColor('#0099ff');
// Send the embed message
await interaction.channel.send({ embeds: [embed] });
} catch (e) {
await interaction.followUp("Something went wrong. Sorry");
console.log(e);
clearInterval(activeSpams.get(spamKey));
activeSpams.delete(spamKey);
}
};
// Start the spam loop
const spamInterval = setInterval(spamLoop, interval * 60 * 1000); // Convert minutes to milliseconds
activeSpams.set(spamKey, spamInterval);
await interaction.reply({ content: "e621spam started!", ephemeral: true });
} else if (action === 'stop') {
if (!activeSpams.has(spamKey)) {
await interaction.reply({ content: "There's no active e621spam in this channel.", ephemeral: true });
return;
}
clearInterval(activeSpams.get(spamKey));
activeSpams.delete(spamKey);
await interaction.reply({ content: "e621spam stopped!", ephemeral: true });
}
},
};
尝试让它重试并创建一个新的 webhook 令牌,但这根本没有帮助。我希望它永远继续发送,直到我使用命令让它停止
Discord 会审核传入 Webhook 的内容,并可能因垃圾邮件和洪水、NSFW 内容不在适当的渠道中或过于频繁地出现 429 错误代码而撤销 Webhook 令牌。
确保您的频道设置“年龄限制频道”已启用。