【问题标题】:I am struggling with dming members with a certain role in discord.py我正在与在 discord.py 中具有一定作用的 dming 成员作斗争
【发布时间】:2021-02-02 15:41:52
【问题描述】:

这是代码

import discord
import random
from discord.ext import commands, tasks
from discord.utils import get

@client.command()
async def play(ctx):
       red_role = discord.utils.get(ctx.message.guild.roles, name="Red")
       blue_role = discord.utils.get(ctx.message.guild.roles, name="Blue")

       red_boss_role = discord.utils.get(ctx.message.server.roles, name="Red Boss")
       blue_boss_role = discord.utils.get(ctx.message.server.roles, name="Blue Boss")

然后私信

for i in red_boss_role_id.members:
    await i.send("????" + str(red_agents))
for i in blue_boss_role_id.members:
    await i.send("????" + str(blue_agents))

我已经用 ID 尝试过同样的事情,但没有进展 它说错误

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Message' object has no attribute 'server'

【问题讨论】:

  • 您在代码中从哪里发送 dm?这只是其中的一小部分,还是整个代码?
  • 好的,我已经编辑过了,抱歉这是我发布的第一个问题。这只是发生错误的代码的一部分

标签: python discord discord.py


【解决方案1】:

我相信你的问题是你使用server,因为它没有定义,它给你一个错误AttributeError: 'Message' object has no attribute 'server',它声明它不是消息的属性。

red_role blue_role 都使用guild,我已将下面的更改为公会,希望这对你有用

@client.command()
async def play(ctx):
       red_role = discord.utils.get(ctx.message.guild.roles, name="Red")
       blue_role = discord.utils.get(ctx.message.guild.roles, name="Blue")

       red_boss_role = discord.utils.get(ctx.message.guild.roles, name="Red Boss")
       blue_boss_role = discord.utils.get(ctx.message.guild.roles, name="Blue Boss")

【讨论】:

    【解决方案2】:

    该错误消息已经说明了一切。出现问题是因为您尝试访问 ctx.message.server.roles,而正确的语法是 ctx.message.guild.roles

    我建议您使用以下代码,因为您的代码非常多余:

    @client.command()
    async def play(ctx):
           red_role = discord.utils.get(ctx.guild.roles, name="Red")
           blue_role = discord.utils.get(ctx.guild.roles, name="Blue")
    
           red_boss_role = discord.utils.get(ctx.guild.roles, name="Red Boss")
           blue_boss_role = discord.utils.get(ctx.guild.roles, name="Blue Boss")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-01
      • 2021-01-15
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多