首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Discord.py模块Python3.6.4踢功能

Discord.py模块Python3.6.4踢功能
EN

Stack Overflow用户
提问于 2018-07-17 03:58:00
回答 1查看 1.7K关注 0票数 2

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

代码语言:javascript
复制
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视频和帖子,与我遇到的问题有关,但似乎没有人有解决方案。我已经写了下面的代码。如果你发现了我漏掉的任何错误,请告诉我。

代码语言:javascript
复制
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')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-17 04:07:24

您似乎正在使用较新的discord.py 1.0,也称为重写分支。You should read this,这是该交换机中所做的许多突破性更改的文档。您还应该只参考该文档,因为旧的0.16版本的大多数文档都不兼容。

许多东西都被移出了Client,转移到了更有意义的地方。具体地说,kick现在是Guild的一种方法。

代码语言:javascript
复制
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方法。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51369259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档