试图使嵌入的不和谐日志记录机器人

时间:2019-11-15 18:21:46

标签: javascript node.js discord.js

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”处的“期望表达”问题。我不知道它要我放在上面什么。

完整代码:

/*
    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 个答案:

答案 0 :(得分:3)

您已经弄乱了代码。

尝试

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

更新了答案,因此您需要与每个服务器建立一个日志通道,并且每当用户删除一条消息时,嵌入内容就会发送到日志通道。