【问题标题】:Discord.py Module Python 3.6.4 kick featureDiscord.py 模块 Python 3.6.4 踢功能
【发布时间】:2018-12-24 10:33:49
【问题描述】:

我正在使用 python 中的 discord 模块制作一个 discord 机器人...我在尝试使 kick 命令工作时遇到了很多麻烦。我尝试使用 bot.kick、client.kick 和 ctx.kick,但它们都给出了相同的错误,即

Ignoring exception in command kick:
Traceback (most recent call last):
  File "C:\Users\user\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 62, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "C:\Users\user\Desktop\Code\bot.py", line 44, in kick
    await client.kick(user)
AttributeError: 'Client' object has no attribute 'kick'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\bot.py", line 886, in invoke
    yield from ctx.command.invoke(ctx)
  File "C:\Users\user\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 493, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\user\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 71, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Client' object has no attribute 'kick'

我尝试搜索与我遇到的问题相关的各种不同的 youtube 视频和帖子,但似乎没有人找到解决方案。我已经写了下面的代码。如果您发现我遗漏的任何错误,请告诉我。

import time
import random
import discord
import asyncio
from discord.ext import commands

#Initialize
client = discord.Client()#Creates Client
bot = commands.Bot(command_prefix='!')#Sets prefix for commands(!Command)

@bot.event
async def on_ready():
    print('Name:', end=' ')
    print(bot.user.name)
    print('ID: ')
    print(bot.user.id)       

@bot.command(pass_context=True)
async def user_info(ctx, user: discord.Member):
    await ctx.send(f'The username of the user is {user.name}')
    await ctx.send(f'The ID of the user is {user.id}')
    await ctx.send(f'The status of the user is {user.status}')
    await ctx.send(f'The role of the user is {user.top_role}')
    await ctx.send(f'The user joined at {user.joined_at}')

@bot.command(pass_context=True)
async def kick(ctx, user: discord.Member):
    await ctx.send(f'The Kicking Hammer Has Awoken! {user.name} Has Been Banished')
    await client.kick(user)

bot.run('SECRET')
client.run('SECRET')

【问题讨论】:

  • 您使用的是什么版本的 discord.py?试试import discord; print(discord.__version__)
  • 问题可能是因为您同时使用了botclient。删除对client 的所有引用,并将它们替换为bot

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


【解决方案1】:

您似乎正在使用更新的discord.py 1.0,也称为重写分支。 You should read this,这是在该开关中进行的许多重大更改的文档。您还应该仅参考该文档,因为旧 0.16 版本的大多数文档不兼容。

很多东西都从Client 移到了更有意义的地方。具体来说,kick 现在是Guilds 的一个方法。

import asyncio
from discord.ext import commands

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

@bot.event
async def on_ready():
    print('Name:', end=' ')
    print(bot.user.name)
    print('ID: ')
    print(bot.user.id)       

@bot.command(pass_context=True)
async def user_info(ctx, user: discord.Member):
    await ctx.send(f'The username of the user is {user.name}')
    await ctx.send(f'The ID of the user is {user.id}')
    await ctx.send(f'The status of the user is {user.status}')
    await ctx.send(f'The role of the user is {user.top_role}')
    await ctx.send(f'The user joined at {user.joined_at}')

@bot.command(pass_context=True)
async def kick(ctx, user: discord.Member):
    await ctx.send(f'The Kicking Hammer Has Awoken! {user.name} Has Been Banished')
    await ctx.guild.kick(user)

bot.run('secret')

请注意,我还删除了上面对client 的所有引用。 BotClient 的子类,因此您可以通过Bot 访问所有Client 方法。

【讨论】:

  • 您也可以使用user.kick(),因为用户是Member 实例。那么你就不需要上下文了!更多信息discordpy.readthedocs.io/en/rewrite/…
  • 非常感谢!您的答案是我在 Google 中找到的唯一工作;)
猜你喜欢
  • 2021-07-27
  • 2021-01-15
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
  • 2021-09-16
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
相关资源
最近更新 更多