python - 为什么我的机器人在加入语音时没有错误?不和谐.py
问题描述
if message.content == "%join":
@client.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
当我输入 %join 时出现错误:
await coro(*args, **kwargs)
File "main.py", line 102, in on_message
@client.command()
AttributeError: 'Client' object has no attribute 'command'
我加入了语音频道,但仍然出现同样的错误。discord.Client() 被重命名为 client 所以它 client = discord.Client() 我做了
解决方案
您应该单独使用 @client.command 而不是在 if 语句中。这是你应该如何做的。
##--Voice Chat Functions--##
@client.command(aliases=['jvc'])
async def joinvc(ctx):
try:
channel = ctx.message.author.voice.channel
except AttributeError:
await ctx.send("You're Not in a Voice Chat Lmao :joy:"
)
global voice
voice = get(client.voice_clients, guild=ctx.guild)
voice = await channel.connect(timeout=3600)
@client.command(aliases=["lvc"])
async def leavevc(ctx):
global voice
channel = ctx.message.author.voice.channel
voice = await voice.disconnect()
如果您在使用 on_message 和命令时遇到问题,请在 on_message 末尾添加await client.process_commands(message)
@client.event
async def on_message(message):
if message.content == "hi":
await message.channel.send("Hi!!!!")
await client.process_commands(message)
@client.command()
...
...
...
推荐阅读
- amazon-web-services - AWS Storage Gateway 文件共享和锁定文件
- laravel - Laravel Eloquent 多重数据透视表
- postgresql - 将 DB2 触发器重写为 PostgreSQL 触发器
- javascript - 获取移动/旋转元素的当前位置
- android - 数据绑定:无法将 ArrayAdpater 绑定到 AppCompatAutoCompleteTextView
- asp.net - 从 WordPress 迁移到 ASP.NET-MVC-5,如何完全删除 WordPress url
- angular - Angular6:如何从命令行生成默认的 angular.json 文件
- python - 无法在 Ubuntu 中启动 Jupyter Notebook
- laravel - 在Controller中调用函数的方式
- .net-core - 在 NLog for .NET Core 应用程序中添加 EventLog 作为目标会导致解析配置错误