2
votes

been trying to get a workaround on this code from another guy but i can't get it to work... here is the code:

import discord
from discord.ext.commands import bot
from discord import game
from discord.ext import commands
import asyncio
import platform
import colorsys
import random
import time

client = commands.Bot(command_prefix = '!', case_insensitive=True)
Client = discord.client
Clientdiscord = discord.Client()

@client.event
async def on_ready():
    print('Logged in as '+client.user.name+' (ID:'+client.user.id+') | Connected to '+str(len(client.servers))+' servers | Connected to '+str(len(set(client.get_all_members())))+' users')
    print('--------')
    print('--------')

@client.command(pass_context = True)
@commands.has_permissions(kick_members=True)     
async def userinfo(ctx, user: discord.Member):
    r, g, b = tuple(int(x * 255) for x in colorsys.hsv_to_rgb(random.random(), 1, 1))
    embed = discord.Embed(title="{}'s info".format(user.name), description="Here's what I could find.", color = discord.Color((r << 16) + (g << 8) + b))
    embed.add_field(name="Name", value=user.name, inline=True)
    embed.add_field(name="ID", value=user.id, inline=True)
    embed.add_field(name="Status", value=user.status, inline=True)
    embed.add_field(name="Highest role", value=user.top_role)
    embed.add_field(name="Joined", value=user.joined_at)
    embed.set_thumbnail(url=user.avatar_url)
    await client.say(embed=embed)

@commands.has_permissions(administrator=True)
@client.command(pass_context = True)
async def send(ctx, *, content: str):
        for member in ctx.message.server.members:
            try:
                await client.send_message(member, content)
                await client.say("DM Sent To : {} :white_check_mark:  ".format(member))
            except:
                print("can't")
                await client.say("DM can't Sent To : {} :x: ".format(member))


client.run("TOKEN") 

The code works to send a DM to EVERYONE in a Discord Server but, what I want is to send DMs to specific roles, i.e: !send role message.

Thanks in advance for the help

PS: It's not a bot for public publishing, I'm just trying to make an efficient announcement system for my guild.

1

1 Answers

2
votes

It looks like you're using a tutorial for the old version of discord.py, or some sort of mesh between the two versions.

There have been some major changes between then and now in the most recent - rewrite - version.

Your command rewritten:

bot = commands.Bot(command_prefix="!", case_insensitive=True)
# you don't need discord.Client()

# this is dming users with a certain role
@commands.has_permissions(administrator=True)
@bot.command()
async def announce(ctx, role: discord.Role, *, msg): # announces to the specified role
    global members
    members = [m for m in ctx.guild.members if role in m.roles]
    for m in members:
        try:
            await m.send(msg)
            await ctx.send(f":white_check_mark: Message sent to {m}")
        except:
            await ctx.send(f":x: No DM could be sent to {m}")
    await ctx.send("Done!")
@announce.error
# feel free to add another decorator here if you wish for it to send the same messages
# for the same exceptions: e.g. @userinfo.error
async def _announcement_error(ctx, error):
    if isinstance(error, commands.BadArgument):
        await ctx.send(":x: Role couldn't be found!")
    elif isinstance(error, commands.MissingPermissions):
        await ctx.send(f":x: {ctx.author.mention}, you don't have sufficient permissions.")
    else:
        await ctx.send(error)

References: