首页 > 解决方案 > 审核日志消息 Discord JS

问题描述

您好 StackOverflow 社区,这是我第一个需要 Discord 帮助的问题,有人可以编辑此代码以使此命令将日志发送到#logs频道,其中包含以下信息 1)标题 - 审核日志 2)用户审核操作是执行于 3) 执行了哪些审核操作(在本例中为禁止)4) 版主 5) 页脚 - Proton Servers Bot©2020


const { client, config, functions } = require('../index.js');

module.exports.run = async (message, u) => {
  if (!message.member.hasPermission('BAN_MEMBERS')) return message.channel.send(functions.errorEmbed('You are not permitted to do this.'));

  if (!u) return message.channel.send(functions.usageEmbed(config.prefix + 'ban <user/id>'));

  const userID = u.match(/[0-9]+/) ? u.match(/([0-9]+)/)[0] : undefined;
  const user = message.guild.members.get(userID);

  if (!user) return message.channel.send(functions.errorEmbed('**' + u + '** is not a valid user.'));

  try {
    await user.ban();
    return message.channel.send(functions.successEmbed('Banned ' + user + ' successfully.'));
  } catch {
    return message.channel.send(functions.errorEmbed('Could not ban ' + user + '. Is their role higher than mine?'));
  }
}

module.exports.description = 'Ban a user.';```

标签: node.jsdiscord.js

解决方案


此答案适用于 discord.js v11。如果您想使用 discord.js v12 执行此操作,请使用MessageEmbed代替RichEmbed并使用cache来获取频道集合message.guild.channels.cache

您可以使用 aRichEmbed创建所需的嵌入,然后将其发送到log频道。

但是,首先,您必须获得log频道,有两种方法可以做到这一点:

按名字:const logChannel = message.guild.channels.find(channel => channel.name === 'log')

或通过 ID:const logChannel = message.guild.channels.get(CHANNEL_ID)

然后你可以用它来发送以下嵌入

const embed = new Discord.RichEmbed() // Or new RichEmbed depending on how you're importing things
  .setTitle('Moderation Log')
  .addField('Banned', user)
  .addField('Banned By', message.author)
  .setFooter('Proton Servers Bot©2020')

logChannel.send(embed)

推荐阅读