簡體   English   中英

Discord Bot 到 DM 特定角色

[英]Discord Bot to DM specific roles

一直試圖從另一個人那里得到這個代碼的解決方法,但我無法讓它工作......這是代碼:

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

該代碼用於向 Discord 服務器中的每個人發送 DM,但是,我想要將 DM 發送到特定角色,即:.send 角色消息。

在此先感謝您的幫助

PS:它不是公共發布的機器人,我只是想為我的公會做一個高效的公告系統。

看起來您正在使用舊版本 discord.py 的教程,或者兩個版本之間的某種網格。

從那時到現在,在最近的重寫版本中發生了一些重大變化

您的命令重寫:

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)

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM