【问题标题】:Discord.js V13 Adding role when using a slash commandDiscord.js V13 使用斜线命令时添加角色
【发布时间】:2021-11-03 12:24:27
【问题描述】:

我正在尝试编写一个机器人,当你使用某个斜杠命令时,它会给你一个角色。我一直在寻找解决方案,但一直找不到。似乎没有什么对我有用,我不断收到有关代码的错误,例如“找不到消息,你的意思是消息吗”

这是我第一次尝试编写机器人代码,所以如果这是一个愚蠢的问题,请多多包涵。

这是我的代码:

import DiscordJS, { Intents, Message, Role } from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()

const client = new DiscordJS.Client({
intents: [
  Intents.FLAGS.GUILDS,
  Intents.FLAGS.GUILD_MESSAGES
],
})

// Slash Commands

//ping pong command

client.on('ready', () => {
console.log('BOT ONLINE')

//bot test server
const guildId = '868857440089804870'
const guild = client.guilds.cache.get(guildId)
const role = client.guilds.cache.find(r => r.name == "Test Role to 
give")

let commands




if (guild) {
  commands = guild.commands
} else {
  commands = client.application?.commands
}




commands?.create({
  name: 'ping',
  description: 'says pong'
})


commands?.create({
  name: "serverip",
  description: 'Gives user the server IP'
})

commands?.create({
  name: "giverole",
  description: 'Gives user the role'
})


})

client.on('interactionCreate', async (interaction) => {
if(!interaction.isCommand()) {
  return
}

const { commandName, options } = interaction
//This is the role id that i want to give
const role = '884231659552116776'
//these are the users that i want to give the role to
const sniper = '725867136232456302'
const josh = '311981346161426433'

if (commandName === 'ping') {
  interaction.reply({
    content: 'pong',
    ephemeral: true,
  })
} else if (commandName === 'serverip') {
interaction.reply({
  content: 'thisisserverip.lol',
  ephemeral: true,
 })
} else if (commandName === 'giverole') {
  interaction.reply({
    content: 'Role given',
    ephemeral: true,
    
   })
  }
 })

 client.login(process.env.test)

【问题讨论】:

  • 您好,欢迎 :D 很抱歉,这不是我们为您编写代码的地方。我们可以帮助您找到现有代码中的错误,但您必须尝试自己实现您的想法^^
  • 我编辑它现在有我的代码

标签: discord.js roles


【解决方案1】:

您只需检索Interaction#Member 并使用GuildMemberRoleManager#add 方法将角色添加到它们!

const role = client.guilds.cache.find(r => r.name == "Test Role to 
give");

await interaction.member.roles.add(role); // and you're all set! welcome to stackoverflow ?

【讨论】:

  • 当我把它放进去时,它给了我这些错误:没有重载匹配这个调用。 ',' 预期
  • 我把“interaction.member.roles.add(role);”放在一起重要吗?在 if 语句中?
  • 不应该,也许可以尝试从 discord.js 导入 GuildMember
  • 这样,对吧?从“discord.js”导入 GuildMember
  • 正确让我知道是否可以解决问题
猜你喜欢
  • 2022-01-07
  • 2022-01-03
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多