【发布时间】: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