【问题标题】:Python discord botsPython 不和谐机器人
【发布时间】:2021-05-27 02:43:40
【问题描述】:

我目前正在尝试创建我的不和谐机器人。可悲的是,这不起作用,我不知道为什么......

import discord
import os
import time
from ka import keep_alive
from discord.ext import commands
import asyncio

client = commands.Bot(command_prefix = '.')
prefix = '.'

@client.event
async def on_ready():
  print("I'm ready! {0.user}".format(client))
  await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Croissants!"))

@client.command()
async def join(ctx):
  channel = ctx.message.author.voice.voice_channel
  await client.join_voice_channel(channel)
  await ctx.send("On my way!")

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

没有错误。但也没有输出。我尝试通过编写使其加入我的 vc:.join

【问题讨论】:

  • 可以分享ka.py的内容吗? (你可能在 repl 上运行它)
  • 我是的。 ka.py 只是每 5 分钟向它发送一次请求,因此机器人不会离线
  • 你是在单独的线程中运行它吗?
  • 让我们把它交给chat.
  • 不能。我需要 20 个声望,我只有 11 个

标签: python discord.py


【解决方案1】:

channel 返回None,因为ctx.message.author 没有voice 属性。此外,Client.join_voice_channel 自 v1.0 起已弃用 (read here)

试试这个:

import discord
import os
import time
from ka import keep_alive
from discord.ext import commands
import asyncio

client = commands.Bot(command_prefix = '.')
prefix = '.'

@client.event
async def on_ready():
  print("I'm ready! {0.user}".format(client))
  await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Croissants!"))

@client.command()
async def join(ctx):
  channel = ctx.guild.get_member(ctx.author.id).voice.channel # This
  await channel.connect()
  await ctx.send("On my way!")

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

【讨论】:

  • 他说没有输出,这意味着 on_ready 甚至没有运行,所以机器人一开始就没有启动。
  • 感谢您的回答。这可能已经解决了它,但我真正的问题是命令事件,甚至根本不被调用如果你有不和谐,你介意在那里说话吗?我们可以更快地回复,更容易发短信
  • 我什至没有注意到你没有加载.env文件,你的令牌在哪里
  • 令牌不是机器人工作的问题,并且已经有一些简单的命令在工作,但我不使用命令事件我使用消息事件
  • @executivemeek 这很奇怪,因为理论上应该可以。您可以尝试使用@commands.command() 代替@client.command() 并添加client.add_command()。如果这不起作用,请使用 on_message 事件
【解决方案2】:

client.add_command(join)。您当前具有命令的功能,但您尚未告诉机器人将其识别为命令。

【讨论】:

  • 只有在使用 commands.command() 装饰器时才需要这样做。 client.command() 注册就好了。
  • 你的意思是('加入')
  • @executivemeek 不,它不需要字符串,因为您之前将其定义为函数
猜你喜欢
  • 2021-10-30
  • 2021-07-15
  • 2021-03-29
  • 2021-07-11
  • 2021-03-25
  • 2018-08-13
  • 1970-01-01
  • 2021-02-28
  • 2021-07-13
相关资源
最近更新 更多