python - Discord Py 无法弄清楚如何添加角色
问题描述
我到处寻找,人们只是告诉我使用文档,但这没有帮助,我已经定义了 on_message(message) 所以我不能使用成员参数,我希望人们被设置为某个当他们说关键字时的角色是我得到的,但它不起作用:
if message.content.lower() == "!changeadmin"
role = discord.utils.get(server.roles, name="Administrator")
await client.add_roles(member, role)
我总是得到回报
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\josep\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:\Users\josep\OneDrive\Documents\New folder\New folder\mybot1.py",
line 58, in on_message
role = discord.utils.get(server.roles, name="admin")
AttributeError: 'str' object has no attribute 'roles'
编辑:这是我的完整代码(不包括底部的令牌):
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
import ctx
Client = discord.Client()
client = commands.Bot(command_prefix = "!")
chat_filter = ["CUNT", "NIGGER", "NIGGA", "FUCK", "BITCH", "DICK", "WANKER"]
bypass_list = ["227007454775476224"]
possible_responses = [
'That is a resounding no',
'It is not looking likely',
'Too hard to tell',
'It is quit possible',
'Definitely',
]
server = '242547220765868033'
roles = ['442351132615114772']
@client.event
async def on_ready():
print("Bot is online and connected to Discord")
bot = commands.Bot(command_prefix='!', description='A bot that greets the user back.')
@client.event
async def on_message(message):
contents = message.content.split(" ") #contents is a list type
for word in contents:
if word.upper() in chat_filter:
if not message.author.id in bypass_list:
try:
await client.delete_message(message)
await client.send_message(channel.message, "**Hey!** You're not allowed to use that word here!")
except discord.errors.NotFound:
return
if message.content.upper().startswith('!PING'):
userID = message.author.id
await client.send_message((discord.Object(id='442333293539622913')), "<@%s> Pong!" % (userID))
if message.content.upper().startswith('!SAY'):
if message.author.id == "227007454775476224" or message.author.id == "399959323591180288":
args = message.content.split(" ")
await client.send_message(message.channel, "%s" % (" ".join(args[1:])))
else:
await client.send_message(message.channel, "Sorry only the bot owner has permission to use this command")
if message.content.lower() == "cookie":
await client.send_message(message.channel, ":cookie:") #responds with Cookie emoji when someone says "cookie"
if message.content.lower() == "!website":
await client.send_message(message.channel, "habbo.com")
if message.content.upper().startswith('!EIGHTBALL'):
await client.send_message(message.channel, random.choice(possible_responses))
if message.content.lower() == '!changeadmin':
role = discord.utils.get(server.roles, name="admin")
await client.add_roles(member, role)
解决方案
抱歉,如果回复晚了 :) 所以如果您想为成员添加角色,请尝试以下代码:
@client.command()
@commands.has_permissions(administrator=True)
async def giverole(ctx,member :discord.Member,role):
try:
add_role= discord.utils.get(ctx.guild.roles, name=role)
await member.add_roles(add_role)
await ctx.send('Updated')
except:
await ctx.send("role not found!")
所以代码会像这样工作:假设前缀=“?”
?giverole @member 示例
机器人将从公会角色中搜索角色名称“示例”并将其提供给@member
推荐阅读
- swagger - OpenAPI 3.0 是否允许引用服务器变量?
- java - Collection扩展了Iterable,但是为什么Collection声明了Iterable Function(和Iterable一样)
- apache-kafka - Kafka Connect 接收精确一次语义的一般方法是什么?
- css - 相对于在 firefox 和 chrome 上工作的父级的大小调整
- apache-spark - 使用 Apache Commons Lang3 SerializationUtils.deserialize 时出现 java.lang.ClassNotFoundException
- flutter - 使用“is”的 Dart 2.1.0 智能转换不起作用
- python - How to properly install Python and Django
- rxjs - 如何在不订阅的情况下使 observable 仍然触发?
- python - 当最后缺少换行符时,codecs.decode 无法解码 uu 编码的字符串
- javascript - 有什么区别以及“&”和“&&”运算符的用途是什么?