Python不和谐机器人

时间:2021-02-24 18:20:47

标签: python discord.py

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

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

2 个答案:

答案 0 :(得分: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'))

答案 1 :(得分:0)

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