【问题标题】:Discord Python Bot unable to set prefixDiscord Python Bot 无法设置前缀
【发布时间】:2021-06-05 21:06:24
【问题描述】:

好的,所以我正在用 Python 设计一个 Discord 机器人,不过还是很新的。我试图让它加入语音频道,所以我做了一些研究并想出了以下内容,我剪掉了机器人的不相关部分,所以这只是语音连接的部分。它说:应该设置前缀的行上的未定义名称“命令”:

import discord
import os
import requests
import json
import random

client = discord.Client()
client = commands.Bot(command_prefix='!')

@client.event
async def on_ready():
    print("Eingeloggt als {0.user}".format(client))

@bot.command()
async def join(ctx):
    channel = ctx.author.voice.channel
    await channel.connect()
@bot.command()
async def leave(ctx):
    await ctx.voice_client.disconnect()

client.run(os.getenv("TOKEN"))

【问题讨论】:

  • 你有client = commands.Bot(command_prefix='!'),然后你写@bot.command()。尝试使用 @client.command() 代替,因为这是您的命令变量或将客户端变量更改为 bot
  • @PhoenixDoom 是的,我也意识到,我改变了它,没有留下任何错误,但它仍然无法工作。
  • 机器人是否有发送消息的权限?
  • @PhoenixDoom 是的,我没有在代码 sn-p 中显示的其他功能按预期工作。
  • 您有on_message 活动吗?有时命令会因此而停止工作。

标签: python python-3.x discord discord.py


【解决方案1】:

你必须使用from discord.ext import commands

import discord
import os
import requests
import json
import random
from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    print("Eingeloggt als {0.user}".format(client))

@bot.command()
async def join(ctx):
    channel = ctx.author.voice.channel
    await channel.connect()
@bot.command()
async def leave(ctx):
    await ctx.voice_client.disconnect()

bot.run(os.getenv("TOKEN"))

【讨论】:

  • 好的,谢谢,我现在没有收到任何错误行了。但是它不起作用,我尝试了简单的命令,例如 ``` @bot.command() async def foo(ctx, arg): await ctx.send(arg) ``` 如果我发送 !foo abc 没有任何返回, 终端里什么都没有,也不和谐
  • 那是一个单独的问题@Apster137。最好问一个新的
猜你喜欢
  • 2023-04-04
  • 2018-01-03
  • 2021-09-28
  • 1970-01-01
  • 2023-01-22
  • 1970-01-01
  • 2021-04-29
  • 2019-06-08
  • 2018-06-28
相关资源
最近更新 更多