简体   繁体   中英

I am struggling with dming members with a certain role in discord.py

Here's the code

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")

and then DMing

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))

I have already tried same thing with IDs, but no progress It says the error

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

That error message already says everything. Your problem arises because you are trying to to access ctx.message.server.roles when the correct syntax would be ctx.message.guild.roles .

I suggest you use the following code, as yours is pretty redundant:

@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")

I believe your problem is you are using server , as it is not defined, it is giving you an error AttributeError: 'Message' object has no attribute 'server' and it states it is not an attribute of message.

red_role and blue_role both were using guild , I've changed the ones below to guild, hopefully this works for you

@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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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