试图用嵌入制作一个不和谐的日志记录机器人

Posted

技术标签:

【中文标题】试图用嵌入制作一个不和谐的日志记录机器人【英文标题】:Trying to make a discord logging bot with embeds 【发布时间】:2020-03-11 22:34:15 【问题描述】:
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)

这是我到目前为止的代码,但是在嵌入的第一部分,我在“.setTitle”或“.setTitle”处遇到了“预期表达式”的问题。我不知道它想让我在上面放什么。

完整代码:

/*
    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) 有点不同,但目前的主要问题是“消息已删除”部分。我正在尝试让它发送嵌入而不是普通消息。

【问题讨论】:

你想在这里实现什么,你能解释一下吗 请用var logger = message.guild.channels.find( channel => channel.name === CHANNEL );替换var logger = message.guild.channels.get(CHANNEL); find 方法已更新,您复制的代码已过时且已弃用。 喜欢这个吧? client.on('messageDelete', message => const CHANNEL = 'log-town'; if (message.channel.type == 'text') var logger = message.guild.channels.find( channel => channel.name === CHANNEL ); channel => channel.name === CHANNEL;不会发送到频道 【参考方案1】:

你的代码有点乱。

试试这个

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');

更新了答案,因此您需要每个服务器都有一个日志通道,并且每当用户删除消息时,嵌入的消息都会发送到日志通道。

【讨论】:

每当我删除消息时,我都会收到:“TypeError: log.setTitle is not a function” @PeppaPapa 我正在我的机器上尝试 discord.js 会给你一些解决方案 @PeppaPapa 根据 discord.js 的文档更新了我的工作,让我知道这是否有效 它没有崩溃,但也没有发送日志。在代码中,它将为您获取频道,这是多服务器设置所需的,因为我很愚蠢,不知道该怎么做。这是原始代码的链接:github.com/richardkriesman/discord-logger/blob/master/server.js 我正在尝试将普通消息更改为嵌入。 @PeppaPapa 基本上你需要指定你想发送嵌入的频道,如果你想把嵌入发送到同一个频道,那么可以不用message.guild.channels.get(CHANNEL);

以上是关于试图用嵌入制作一个不和谐的日志记录机器人的主要内容,如果未能解决你的问题,请参考以下文章

如何为不和谐的机器人制作一个可变的嵌入列表?

使用 discord.py 制作一个不和谐的机器人通过嵌入发送图像

试图让一个机器人与 Selenium Python 建立不和谐的连接

在heroku上构建错误;试图制作一个不和谐的机器人

不和谐机器人日志

试图为不和谐制作一个聊天机器人,但它不起作用