如果语句与消息作者角色不满

发布于 2025-01-22 08:45:55 字数 3231 浏览 0 评论 0 原文

我知道我一定不能成为第一个尝试这样做的人。但是我真的找不到解决问题的解决方案。这是我想做的。我只有在角色为“成员”时才尝试在频道中打印会员角色。问题是,我无法获得角色并在if语句中使用。我尝试了很多事情,直到我最终理解对象客户端没有get_context方法。但是每个人都在使用它,似乎能够使事情起作用。我已经阅读了官方的API文档,并且可以清楚地看到对象“客户端”没有GET_CONTEXT方法,但是Ext.Command.bot对象可以。我该怎么做才能使事情起作用?

这是我遇到的错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/home/runner/Discord-Moderator/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 21, in on_message
    ctx = await client.get_context(message)
AttributeError: 'Client' object has no attribute 'get_context'

这是我的代码:

import discord
import os
import random
from stayin_alive import stayin_alive

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):
    msg = message.content.lower()
    ctx = await client.get_context(message)

    if message.author == client.user:
        return

          role = discord.utils.find(lambda r: r.name == 'Member', ctx.message.server.roles)
          if role in message.author.roles:
            await message.channel.send("You are a member")

我什至尝试了命令的事情,以便像这样做 discord.py bot.get_context()不返回ctx对象 /strong>

import commands
from stayin_alive import stayin_alive

client = discord.Client()
bot = commands.Bot(command_prefix=("!sm"))

,但我得到了这个错误:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    import commands
ModuleNotFoundError: No module named 'commands'

这是我检查的一切: https:// https:///www.codegrepper.com/ code-examples/python/discord.py+add+row+to+用户

https://discordpy.readthedocs.io/en/stable/ext/commands/api.html (bot命令对象官方doc)

https://discordpy.readthedocs.io/en/stable/api.html (客户端对象官方doc)

discord.py bot.get_context()不返回ctx对象

a href =“ https://www.reddit.com/r/learlenpython/comments/o8jpi6/so_im_im_making_a_discord_bot/” rel =“ nofollow noreferrer” so_im_making_a_discord_bot/

我如何从on_message事件中获得一个不和谐的上下文对象?

有人可以帮助我找到解决问题的解决方案。我只是想在我的if语句中使用message.author.role。

I know that I must not be the first one trying to do this. But I really cannot find the solution to my problem. Here is what i'm trying to do. I'm trying to print the member role in the channel only if is role is "member". The thing is, i'm not able to get is role and use it in the IF statement. I tried a lot of thing until I finally understood that the object Client doesn't have a get_context method. But everyone is using it and seems to be able to make things work. I've read the official API docs and I can clearly see that the object "Client" doesn't have a get_context method, but the ext.command.Bot object does. What can I do to make things work ?.

Here is the error I get:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/home/runner/Discord-Moderator/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 21, in on_message
    ctx = await client.get_context(message)
AttributeError: 'Client' object has no attribute 'get_context'

Here is my code:

import discord
import os
import random
from stayin_alive import stayin_alive

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):
    msg = message.content.lower()
    ctx = await client.get_context(message)

    if message.author == client.user:
        return

          role = discord.utils.find(lambda r: r.name == 'Member', ctx.message.server.roles)
          if role in message.author.roles:
            await message.channel.send("You are a member")

I even tried with the commands thing in order to do it like this Discord.py bot.get_context() does not return ctx object

import commands
from stayin_alive import stayin_alive

client = discord.Client()
bot = commands.Bot(command_prefix=("!sm"))

But I get this error:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    import commands
ModuleNotFoundError: No module named 'commands'

Here is everything I checked out:
https://www.codegrepper.com/code-examples/python/discord.py+add+role+to+user

https://discordpy.readthedocs.io/en/stable/ext/commands/api.html (Bot command Object official Doc)

https://discordpy.readthedocs.io/en/stable/api.html (Client Object Official Doc)

Discord.py bot.get_context() does not return ctx object

https://www.reddit.com/r/learnpython/comments/o8jpi6/so_im_making_a_discord_bot/

How can I get a Discord Context Object from the on_message event?

Can someone please help me to find the solution to my problem. I simply wanna use the message.author.role in my IF statement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2025-01-29 08:45:55

这是解决方案,我终于找到了,我不那么远,我知道我可以在不使用CTX的情况下做到这一点

enter image description here

Here is the solution, I've finally found it, I wasn't that far, I knew I could do it without using ctx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文