简体   繁体   English

根据命令提供角色的 Discord Bot 不起作用

[英]Discord Bot that gives roles on command doesn't work

I am trying to make a Discord bot in Python that gives people roles on command.我正在尝试用 Python 制作一个 Discord 机器人,它可以根据命令为人们提供角色。 I tried this:我试过这个:

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content == '$attack':
        role = discord.utils.get(message.guild.roles, name='Do admin?')
        await member.add_roles(message.author, role)


client.run('MyToken!')

When I run the command $attack I get this:当我运行命令 $attack 我得到这个:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/home/pandemicofnukes/PycharmProjects/bot/venv/lib/python3.7/site-packages/discord/client.py", line 271, in _run_event
    await coro(*args, **kwargs)
  File "/home/pandemicofnukes/PycharmProjects/bot/bot.py", line 31, in on_message
    await member.add_roles(message.author, role)
NameError: name 'member' is not defined

I don't have more ideas to get this working.我没有更多的想法来让这个工作。

You're using syntax from a version of discord.py, v0.16, that isn't supported anymore.您正在使用 discord.py v0.16 版本中的语法,该版本不再受支持。
See the guide for migrating to v1 .请参阅迁移到 v1 的指南

client.add_roles(message.author, role) is returning None without being awaited. client.add_roles(message.author, role)正在返回None而不等待。
It's impossible to determine why this is the case without seeing more of your code.如果不查看更多代码,就不可能确定为什么会出现这种情况。

Well problem resolved, I figured out...It was literally to add from discord import Member in async def on_message(message):问题解决了,我想通了...实际上是在async def on_message(message):添加from discord import Member async def on_message(message):

Thank you all for trying to help me!感谢大家试图帮助我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM