【问题标题】:Discord.py get user returns noneDiscord.py 获取用户返回无
【发布时间】:2021-01-01 16:28:49
【问题描述】:

我想禁止任何成员使用此代码发送消息;

elif "$ban" in message.content:
    msg = message.content
    msgsplit = msg.split() #banid[-1] gets last index of message array
    banidstr = msgsplit[-1]
    banid = int(banidstr)
    member = bot.get_user(int(banid))
    print(member)

它看起来像意大利面条,但用于测试。 “print (member)”行不返回任何内容,但“print (banid)”返回用户 ID。我能做什么?

【问题讨论】:

  • 您是否启用了intents.members
  • 什么是“intents.members”?我现在再次尝试,当我手动输入 id 时,成员正确打印。但无法从消息中获取成员。我的意思是,“bot.get_user(int(banid))”不起作用。但是“bot.get_user(12121121212121212)”工作。这个 id 是随机的。
  • 我从字面上复制并粘贴了您的代码来测试它,它运行良好。你能展示整个on_message() 事件吗?也许错误在其他地方。
  • imgur.com/D3j1fQl 整个活动。

标签: python discord.py


【解决方案1】:

我认为this 的帖子可能会有所帮助。

我也推荐你使用discord.ext.commands.Bot(),在这里制作命令更容易:

from discord.ext import commands

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

@bot.command()
async def echo(ctx, *, message):
    await ctx.send(message)

# Command will be invoked by user like '?echo this is the bot speaking'
# And the bot will then send 'this is the bot speaking'

【讨论】:

  • 谢谢,这绝对更容易。我会用的。
【解决方案2】:

是的,伙计们,它起作用了;

async def ceza(ctx, *, message):
    cezaname=bot.get_user(int(message))
    print(cezaname)

这个,按 id 正确打印用户名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 2020-07-21
    • 2021-07-18
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    相关资源
    最近更新 更多