当前位置: 首页 > 知识库问答 >
问题:

试图用嵌入式创建一个不和谐的日志记录机器人

华景焕
2023-03-14
bot.on('messageDelete', function(message) {

    if(message.channel.type == 'text') {

        //log to console
        console.log('[' + message.guild.name + '][#' + message.channel.name + '][DELMSG] ' + message.author.username +
            '#' + message.author.discriminator + ': ' + formatConsoleMessage(message));

        //post in the guild's log channel
        var log = message.guild.channels.find('name', CHANNEL);
        if (log != null)
            .setTitle("Message Deleted")
            .addField("Author", message.author.username)
            .addField("Message", message.cleanContent)
            .setThumbnail(message.author.avatarURL)
            .setColor("0x00AAFF");
            log.sendEmbed(embed)

这是我到目前为止的代码,但atm我在“.settitle”oor处出现了“Expression expected”的问题,只是在嵌入的第一节。我不知道它想让我把什么放在上面。

完整代码:

/*
    Logger v0.1.0
    A bot for Discord that logs moderator actions.
    -----------------------------------------------
    Copyright © Richard Kriesman 2016.
*/

//imports

//constants
const VERSION = '0.1.1';
//const TOKEN = 'TOKEN GOES HERE';
const CHANNEL = 'log-town';

//declarations
//var bot = new Discord.Client();

//
// Event Handlers
//

//bot is ready to start working, print status update to console
bot.on('ready', function() {
    console.log('[META][INFO] Connected to Discord API Service');
});

//bot disconnected from Discord
bot.on('disconnected', function() {
    console.log('[META][WARN] Disconnected from Discord API Service. Attempting to reconnected...');
});

//warning from Discord.js
bot.on('warn', function(msg) {
    console.log('[META][WARN] ' + msg);
});

//error from Discord.js
bot.on('error', function(err) {
    console.log('[META][ERROR] ' + err.message);
    process.exit(1);
});

//message received
bot.on('message', function(message) {
    if(message.author.id != bot.user.id) {
        if (message.channel.type == 'text')
            console.log('[' + message.guild.name + '][#' + message.channel.name + '][MSG] ' + message.author.username +
                '#' + message.author.discriminator + ': ' + formatConsoleMessage(message));
        else if (message.channel.type == 'dm')
            message.channel.sendMessage('Beep boop! Sorry, I can\'t respond to direct messages. Try inviting me to your ' +
                'server!\nhttps://discordapp.com/oauth2/authorize?client_id=643707791910895634&scope=bot&permissions=8');
        else if (message.channel.type == 'group')
            message.channel.sendMessage('Beep boop! Sorry, I can\'t log group messages. Try inviting me to your server!\n' +
                'https://discordapp.com/oauth2/authorize?client_id=643707791910895634&scope=bot&permissions=8');
    }
});

//message deleted
client.on('messageDelete', message => {
    if (message.channel.type == 'text') {
      var logger = message.guild.channels.get(CHANNEL);
      if (logger) {
        const embed = new Discord.RichEmbed()
          .setTitle('Message Deleted')
          .addField('Author', message.author.username)
          .addField('Message', message.cleanContent)
          .setThumbnail(message.author.avatarURL)
          .setColor('0x00AAFF');
        logger.send({ embed });
      }
    }
  });

                                        //log.sendMessage('**[Message Deleted]** ' + message.author + ': ' + message.cleanContent);



//message update
bot.on('messageUpdate', function(oldMessage, newMessage) {

    if (newMessage.channel.type == 'text' && newMessage.cleanContent != oldMessage.cleanContent) {

        //log to console
        console.log('[' + newMessage.guild.name + '][#' + newMessage.channel.name + '][UPDMSG] ' +
            newMessage.author.username + '#' + newMessage.author.discriminator + ':\n\tOLDMSG: ' +
            formatConsoleMessage(oldMessage) + '\n\tNEWMSG: ' + formatConsoleMessage(newMessage));

        //post in the guild's log channel
        var log = newMessage.guild.channels.find('name', CHANNEL);
        if (log != null)
            log.sendMessage('**[Message Updated]** *' + newMessage.author + '*:\n*Old Message*: ' + oldMessage.cleanContent +
                '\n*New Message*: ' + newMessage.cleanContent);
    }

});

//user has been banned
bot.on('guildBanAdd', function(guild, user) {

    //log to console
    console.log('[' + guild.name + '][BAN] ' + user.username + '#' + user.discriminator);

    //post in the guild's log channel
    var log = guild.channels.find('name', CHANNEL);
    if (log != null)
        log.sendMessage('**[Banned]** ' + user);

});

//user has been unbanned
bot.on('guildBanRemove', function(guild, user) {

    //log to console
    console.log('[' + guild.name + '][UNBAN] ' + user.username + '#' + user.discriminator);

    //post in the guild's log channel
    var log = guild.channels.find('name', CHANNEL);
    if (log != null)
        log.sendMessage('**[Unbanned]** ' + user);

});

//user has joined a guild
bot.on('guildMemberAdd', function(guild, user) {

    //log to console
    console.log('[' + guild.name + '][JOIN] ' + user.username + '#' + user.discriminator);

    //post in the guild's log channel
    var log = guild.channels.find('name', CHANNEL);
    if (log != null) {
        log.sendMessage('**[Joined]** ' + user);
    }

});

//user has joined a guild
bot.on('guildMemberRemove', function(guild, user) {

    //log to console
    console.log('[' + guild.name + '][LEAVE] ' + user.username + '#' + user.discriminator);

    //post in the guild's log channel
    var log = guild.channels.find('name', CHANNEL);
    if (log != null)
        log.sendMessage('**[Left]** ' + user);

});

//user in a guild has been updated
bot.on('guildMemberUpdate', function(guild, oldMember, newMember) {

    //declare changes
    var Changes = {
        unknown: 0,
        addedRole: 1,
        removedRole: 2,
        username: 3,
        nickname: 4,
        avatar: 5
    };
    var change = Changes.unknown;

    //check if roles were removed
    var removedRole = '';
    oldMember.roles.every(function(value) {
        if(newMember.roles.find('id', value.id) == null) {
            change = Changes.removedRole;
            removedRole = value.name;
        }
    });

    //check if roles were added
    var addedRole = '';
    newMember.roles.every(function(value) {
        if(oldMember.roles.find('id', value.id) == null) {
            change = Changes.addedRole;
            addedRole = value.name;
        }
    });

    //check if username changed
    if(newMember.user.username != oldMember.user.username)
        change = Changes.username;

    //check if nickname changed
    if(newMember.nickname != oldMember.nickname)
        change = Changes.nickname;

    //check if avatar changed
    if(newMember.user.avatarURL != oldMember.user.avatarURL)
        change = Changes.avatar;

    //log to console
    switch(change) {
        case Changes.unknown:
            console.log('[' + guild.name + '][UPDUSR] ' + newMember.user.username + '#' + newMember.user.discriminator);
            break;
        case Changes.addedRole:
            console.log('[' + guild.name + '][ADDROLE] ' + newMember.user.username +'#' +  newMember.user.discriminator +
                ': ' + addedRole);
            break;
        case Changes.removedRole:
            console.log('[' + guild.name + '][REMROLE] ' + newMember.user.username + '#' + newMember.user.discriminator +
                ': ' + removedRole);
            break;
        case Changes.username:
            console.log('[' + guild.name + '][UPDUSRNM] ' + oldMember.user.username + '#' + oldMember.user.discriminator +
                ' is now ' + newMember.user.username + '#' + newMember.user.discriminator);
            break;
        case Changes.nickname:
            console.log('[' + guild.name + '][UPDUSRNK] ' + newMember.user.username + '#' + newMember.user.discriminator +
                (oldMember.nickname != null ? ' (' + oldMember.nickname + ')' : '') +
                (newMember.nickname != null ? ' is now ' + newMember.nickname : ' no longer has a nickname.'));
            break;
        case Changes.avatar:
            console.log('[' + guild.name + '][UPDAVT] ' + newMember.user.username + '#' + newMember.user.discriminator);
            break;
    }


    //post in the guild's log channel
    var log = guild.channels.find('name', CHANNEL);
    if (log != null) {
        switch(change) {
            case Changes.unknown:
                log.sendMessage('**[User Update]** ' + newMember);
                break;
            case Changes.addedRole:
                log.sendMessage('**[User Role Added]** ' + newMember + ': ' + addedRole);
                break;
            case Changes.removedRole:
                log.sendMessage('**[User Role Removed]** ' + newMember + ': ' + removedRole);
                break;
            case Changes.username:
                log.sendMessage('**[User Username Changed]** ' + newMember + ': Username changed from ' +
                    oldMember.user.username + '#' + oldMember.user.discriminator + ' to ' +
                    newMember.user.username + '#' + newMember.user.discriminator);
                break;
            case Changes.nickname:
                log.sendMessage('**[User Nickname Changed]** ' + newMember + ': ' +
                    (oldMember.nickname != null ? 'Changed nickname from ' + oldMember.nickname +
                        + newMember.nickname : 'Set nickname') + ' to ' +
                    (newMember.nickname != null ? newMember.nickname + '.' : 'original username.'));
                break;
            case Changes.avatar:
                log.sendMessage('**[User Avatar Changed]** ' + newMember);
                break;
        }
    }

});

//
// Startup Sequence
//
console.log('Logger v' + VERSION);
console.log('A bot for Discord that logs moderator actions.\n');
console.log('Copyright © Richard Kriesman 2016. Released under the MIT license.');
console.log('----------------------------------------------');

console.log('[META][INFO] Started Logger v' + VERSION);

//bot.login(TOKEN); //log in to discord

function formatConsoleMessage(message) {
    return message.cleanContent.replace(new RegExp('\n', 'g'), '\n\t');
}

我放入了完整的代码,因为它与原始代码(https://github.com/richardkriesman/discord-logger/blob/master/server.js)有些不同,但目前的主要问题是“消息删除”部分。我想让它发送嵌入式而不是普通的消息。

共有1个答案

公羊兴文
2023-03-14

你的代码有点乱。

试试这个

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageDelete', message => {
  const CHANNEL = 'log';
  if (message.channel.type == 'text') {
    var logger = message.guild.channels.find(
      channel => channel.name === CHANNEL
    );
    if (logger) {
      const embed = new Discord.RichEmbed()
        .setTitle('Message Deleted')
        .addField('Author', message.author.username)
        .addField('Message', message.cleanContent)
        .setThumbnail(message.author.avatarURL)
        .setColor('0x00AAFF');
      logger.send({ embed });
    }
  }
});

client.login('YOUR TOKEN');

更新了答案,所以您需要与每个服务器有一个日志通道,并且每当用户删除消息时,嵌入将被发送到日志通道。

 类似资料:
  • 我想更改我的嵌入式数据库的日志记录。每次我打开一个连接,atm都会记录“......创建新的JDBC驱动程序连接到......”。在我的测试套件中,我为每个请求打开一个新的连接,现在有很多这样的消息。 我没有找到任何类型的日志级别。我只是可以设置PrintWriter。但是我想看看错误msgs。我在Spring之外运行这个作为常规单元测试。 有什么想法吗?

  • 一般来说,你应该在运行时增加调试选项来调试问题;也可以把调试选项添加到 Ceph 配置文件里来调试启动问题,然后查看 /var/log/ceph (默认位置)下的日志文件。 Tip 调试输出会拖慢系统,这种延时有可能掩盖竞争条件。 日志记录是资源密集任务。如果你碰到的问题在集群的某个特定区域,只启用那个区域对应的日志功能即可。例如,你的 OSD 运行良好、元数据服务器却不行,这时应该先打开那个可疑

  • ,日志记录将进入一个文件; (路径)/service_name/service_name.log 我想用logback复制这种行为,但在logback.xml配置中获取“logger”名称时遇到了真正的困难。它可以在log encoder.pattern中看到,即“%d%-5level%logger{35}-%msg%n”。

  • 我想在我的应用程序中使用SLF4J+logback用于两个目的--日志和审计。 14:41:57.978[main]信息AUDIT_LOGGER-110欢迎使用main 如何确保审核消息在审核记录器下只出现一次?

  • 我使用的是播放框架,其中日志是默认的记录器引擎。为了便于跟踪,我希望将两个不同的活动(和)记录在两个单独的文件中。为此,我有两个追加器和两个记录器。每个记录器都用特定的appender标记。 我希望通过事务记录器(如登录或注册相关活动(如 但不管用。有什么想法吗? 我所说的“不工作”是指相同的日志被附加到两个记录器中,而不是特定于特定的日志。例如。如果我从播放代码调用,事务和登录记录器都将用户X已

  • 我试图做一个程序来创建一个无尽的循环,例如: 但是主要的问题是一个机器人听不到另一个机器人的声音,所以这不起作用。。。 有没有办法让机器人听另一个机器人的?提前感谢!:)