我想要一个命令,它发送一个包含机器人所在的所有服务器的嵌入

时间:2021-02-01 04:58:30

标签: javascript node.js discord discord.js bots

我创建了一个 discord.js bot,一段时间后我想添加一个服务器列表命令 为每个服务器发送带有嵌入的消息,其中包含:Server NameMember CountServer Avatar (As The Embed Thumbnail)Server Owner ID,最重要的是我不希望任何人能够使用它除了我之外的命令,所以也许我用我的 ID 添加了一个常量?, 我真的想不出它的代码,但无论如何......这是其中一个命令的格式:

if((args[0] === settings.prefix + "deletereactions" || args[0] === settings.prefix + "dr") && msg.member.hasPermission("ADMINISTRATOR")){
        //deletereactions #channel
        let channel = msg.mentions.channels.array()[0];
        if(!channel) return;
        let db = client1.db("rbot");
        let collection = db.collection("reactions");
        collection.deleteOne({channel : channel.id}, function(err){
            if(err) console.log(err);
            msg.reply("**✅ Done!**");
        })
    }
})

这是我的命令处理程序:

const settings = require("./settings.json");
const Discord = require("discord.js");
const client = new Discord.Client();
const fs = require("fs");
const MongoClient = require("mongodb").MongoClient;
const url = "my_mongodb_url";
const mongoClient = new MongoClient(url, { useUnifiedTopology: true });
const moment = require("moment");
const { CommandCursor } = require("mongodb");

let client1;


client.login(settings.token);

client.on("ready", ready =>{
    console.log("Ready");
    mongoClient.connect(function(err, cli){
        client1 = cli;
    })
    client.user.setActivity(`${settings.activity}`, { type: 'LISTENING' })
})

client.on("message", async msg =>{
    let args = msg.content.split(' ');
    if(msg.channel.type !== "text") return;
    if(msg.channel.type === "text"){
        let db = client1.db("rbot");
        let collection = db.collection("reactions");
        collection.findOne({channel : msg.channel.id}, function(err, result){
            if(err) console.log(err);
            if(result){
                for(let i = 0; i < result.reactions.length; i++){
                    msg.react(result.reactions[i]).catch(err =>{
                        if(err) collection.deleteOne({channel : msg.channel.id}, function(err){
                            if(err) console.log(err);
                        })
                    });

2 个答案:

答案 0 :(得分:0)

要获取机器人所在的所有公会,您可以使用 js 地图。

// You can change ', ' to '/n' if you want paragraph breaks instead.

let guilds = bot.guilds.cache.map(g => g.name).join(', ');
message.channel.send(guilds, { split:true })

// Split:true makes the message split into multiple messages in case it reaches the word limit of 2000.

答案 1 :(得分:0)

在您的命令中(运行或执行函数): 对于 1 个用户:

if (message.author.id != "YOUR ID") return;

对于 2+ 个用户:

let Owners = ["ID 1", "ID 2"];
if (!Owners.includes(message.author.id)) return;

例如:

module.exports = {
  name: "eval",
  run: async (client, message, args) => {
    if (message.author.id != "696969696969") return message.channel.send("Only Owners Can Use Eval Command!");

   //...
  }
};

链接:
User#id
Array