【问题标题】:Create a channel with discord.js使用 discord.js 创建频道
【发布时间】:2023-03-27 02:21:01
【问题描述】:

我正在尝试做一个不和谐的机器人来创建频道等,但我无法实现它,因为我的代码应该创建一个频道但是...我尝试了很多东西,这两个没有'没有给我错误:(我用 DONOTLOOK 替换了机器人的令牌,我不会有问题......)

选项 1:

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

bot.login('DONOTLOOK');

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

bot.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
    var server = msg.guild;
    bot.channels.add("anewchannel", {type: 0});
  }
});

选项 2:

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

bot.login('DONOTLOOK');

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

bot.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
    var server = msg.guild;
    const channel = bot.channels.add("newchannel", {type: 0});
  }
});

这并没有在控制台上给我任何 node.js 错误,机器人回答了我但没有创建频道。我还查看了两个参考(https://discord.com/developers/docs/resources/guild#create-guild-channelhttps://discord.js.org/#/docs/main/stable/class/GuildChannelManager?scrollTo=create),没有人的解决方案有效,discord.com 的一个也给了我 node.js 错误。上面的两个选项取自 discord.js 文件,它们没有给出 node.js 错误,但没有创建通道。如果有人可以帮助我,请。

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    下面的代码解释了如何通过命令创建一个文本通道。

    bot.on('message', msg => { //Message event listener
        if (msg.content === 'channel') { //If the message contained the command
            message.guild.channels.create('Text', { //Create a channel
                type: 'text', //Make sure the channel is a text channel
                permissionOverwrites: [{ //Set permission overwrites
                    id: message.guild.id,
                    allow: ['VIEW_CHANNEL'],
                }]
            });
            message.channel.send("Channel Created!); //Let the user know that the channel was created
        }
    });
    

    【讨论】:

      【解决方案2】:

      discord.js v13 改变了一些东西。

      更新版本:

      bot.on("messageCreate", message => { // instead of 'message', it's now 'messageCreate'
          if (message.content === "channel") {
              message.guild.channels.create("channel name", { 
                  type: "GUILD_TEXT", // syntax has changed a bit
                  permissionOverwrites: [{ // same as before
                      id: message.guild.id,
                      allow: ["VIEW_CHANNEL"],
                  }]
              });
              message.channel.send("Channel Created!);
      })
      

      不相关,但我建议使用bot.guilds.cache.get(message.guildId) 而不是直接获取消息的公会。使用正则表达式也远远优于message.content === "channel"

      【讨论】:

        【解决方案3】:

        如果你不想使用消息来创建你可以使用..

        var bot = client.guilds.cache.get("<GUILD-ID>");
          server.channels.create("<CHANNEL-TEXT>", "<CHANNEL-TYPE>").then(channel => {
            var cat = server.channels.cache.find(c => c.name == "BUSINESSES" && c.type == "category");
            if (!cat) { console.log("category does not exist"); return; }
            channel.setParent(cat.id);
          })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-04-04
          • 1970-01-01
          • 2021-05-04
          • 2021-10-12
          • 2018-03-04
          • 2020-06-23
          • 2020-07-26
          • 1970-01-01
          相关资源
          最近更新 更多