繁体   English   中英

Discord.js V14 / 重启机器人后嵌入不发送

[英]Discord.js V14 / Embed doesn't send after restarting bot

短篇故事:

我用/setup-blacklistlog设置日志通道然后我将用户 A列入黑名单然后让用户 A加入服务器并且代码向我发送一个嵌入它检测到黑名单用户的原因但是在我重新启动之后让用户再次加入有没有发送嵌入,当我再次尝试设置频道时,它没有说:“这已经存在。” (当然这并没有在实际代码中实现。)

代码:

const { EmbedBuilder, SlashCommandBuilder, PermissionFlagsBits } = require("discord.js");
const Blacklist = require("../../Schemas/BlackListUser");
const LogChannel = require("../../Schemas/LogChannel");

module.exports = {
    data: new SlashCommandBuilder()
        .setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
        .setName("setup-blacklistlog")
        .setDescription("Set up your blacklist log for the discord bot.")
        .addChannelOption(option =>
            option.setName("channel")
                .setDescription("Channel for blacklist logs.")
                .setRequired(true)
        ),

    async execute(interaction) {
        const { client, channel, guildId, options } = interaction;
        const blacklistChannel = options.getChannel("channel") || channel;

        // Save the channel to the database
        let logChannel = new LogChannel({
            Guild: guildId,
            Channel: blacklistChannel.id
        });
        await logChannel.save();

        // Send a message to the channel confirming setup
        let embed = new EmbedBuilder()
            .setColor("Green")
            .setDescription("Successfully set up this server's blacklist log channel.")
            .setTimestamp();
        interaction.reply({ embeds: [embed], ephemeral: true });

        // Check for blacklisted users when they join the server
        client.on("guildMemberAdd", async (member) => {
            LogChannel.findOne({ Guild: member.guild.id }, async (err, logData) => {
                if (err) {
                    console.error(err);
                    return;
                }
                if (logData) {
                    Blacklist.findOne({ User: member.id }, async (err, data) => {
                        if (err) {
                            console.error(err);
                            return;
                        }
                        if (data) {
                            let blacklistChannel = member.guild.channels.cache.get(logData.Channel);
                            let embed = new EmbedBuilder()
                                .setColor("Red")
                                .setTitle("User located in Blacklist Database!")
                                .addFields(
                                    { name: "User", value: `<@${data.User}>` },
                                    { name: "Reason", value: `${data.Reason}` },
                                )
                                .setTimestamp();
                            blacklistChannel.send({ embeds: [embed] });
                        }
                    });
                }
            });
        });
    }
}

控制台中没有错误。

尝试通过将文件分成四个来修复它,一个用于更新、检查、添加和实际命令。 -> 没有用。

尝试在线搜索帮助和 Discord。

当您重新启动您的程序时,您的黑名单频道和列入黑名单的个人是空白/空的/不存在的,因为机器人不会永久保存它们。 它仅在机器人运行时保存,并在您停止机器人时丢失/删除。 因此,您必须将数据保存在数据库中(如 mongodb、mySQL 或 ),并在每次启动机器人时获取该数据。 是 discordjs 使用数据库的文档。 它使用 Sequelize 和我猜的某种工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM