Bot与DM无关的角色
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bot与DM无关的角色相关的知识,希望对你有一定的参考价值。
试图从另一个人那里获得有关此代码的解决方法,但我无法使其正常工作……这是代码:
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")
该代码可以将DM发送给Discord Server中的每个人,但是,我想要的是将DM发送给特定角色,即:!发送角色消息。
感谢您的帮助
PS:它不是公开发布的机器人,我只是想为我的公会建立一个高效的公告系统。
答案
似乎您正在使用旧版本的discord.py的教程,或这两个版本之间的某种网格。从那时到现在,最近的版本-重写-版本中存在一些major changes。
您的命令已重写:
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
members = [m for m in ctx.guild.members if role in m.roles]
for m in members:
await m.send(msg)
try:
await ctx.send(f":white_check_mark: Message sent to m")
except:
await ctx.send(f":x: Message couldn't be sent to m")
await ctx.send("Done!")
@announce.error
async def _announcement_error(ctx, error):
if isinstance(error, commands.BadArgument):
await ctx.send("Role couldn't be found!")
elif # catch another error if you wish
# do some stuff
else:
await ctx.send(error)
参考:
commands.Context
commands.Context
commands.has_permissions()
- [
commands.has_permissions()
-捕获特定命令的错误] Member.roles
- [
Member.roles
-Python 3.6 +
参考:
commands.Context
commands.has_permissions()
- [
commands.has_permissions()
-捕获特定命令的错误] Member.roles
- [
Member.roles
-Python 3.6 +
commands.Context
以上是关于Bot与DM无关的角色的主要内容,如果未能解决你的问题,请参考以下文章