当用户添加反应 Discord.py 时错误赋予角色
Posted
技术标签:
【中文标题】当用户添加反应 Discord.py 时错误赋予角色【英文标题】:Error give role when a user add reaction Discord.py 【发布时间】:2019-05-31 18:22:41 【问题描述】:但对我不起作用..,
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from discord import Game
import math, time
from discord.ext import commands
from discord.utils import get
Client = discord.client
client = commands.Bot(command_prefix = '!')
Clientdiscord = discord.Client()
@client.event
async def on_ready():
Channel = client.get_channel('524415641310986252')
Text= "testt!"
Moji = await client.send_message(Channel, Text)
await client.add_reaction(Moji, emoji='\U0001F3D3')
client.loop.create_task(on_reaction_add())
@client.event
async def on_reaction_add(reaction, user):
Channel = client.get_channel('524415641310986252')
if reaction.message.channel.id != Channel:
return
if reaction.emoji == "\U0001F3D3":
Role = discord.utils.get(user.server.roles, name="verified")
await client.add_roles(user, Role)
await client.add_roles(reaction.message.author, role)
client.run("My_Token")
\U0001F3D3 = :ping_pong: ,使用 unicode
here picture for example
所以它工作,但给了我一些错误
忽略 on_ready 中的异常 回溯(最近一次通话最后): _run_event 中的文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py”,第 307 行 来自 getattr(self, event)(*args, **kwargs) 文件“C:\Users\Administrator\Desktop\project_Bot_manage - - Copy - Copy.py”,第 24 行,在 on_ready client.loop.create_task(on_reaction_add()) TypeError: on_reaction_add() 缺少 2 个必需的位置参数:'reaction' 和 'user'
我需要帮助,我还是 discord.py 的新手
【问题讨论】:
我也试过了,但没用:***.com/questions/52210855/… 这条线应该做什么client.loop.create_task(on_reaction_add())
?你不需要那条线
@PatrickHaugh 好的,我删除了那行,(没有问题显示+没有给我这个角色。)
reaction.message.channel.id != Channel
您应该比较两个 Channel
对象,而不是带有字符串的 Channel
对象。试试reaction.message.channel != Channel
@PatrickHaugh 成功了,谢谢,你能把它发布为“回答问题”吗,请你添加一会儿我删除我的反应会删除我的角色,我试过如果反应.emoji == "\U0001F3D3": Role = discord.utils.get(user.server.roles, name="verified") await client.add_roles(user, Role) await client.add_roles(reaction.message.author, Role) else: await bot.remove_roles(reaction.message.author,Role) if 在reaction.message.author.roles 中的角色:await client.remove_roles(reaction.message.author,Role) else:await client.add_roles(reaction.message.author,Role )
【参考方案1】:
当用户删除他们的反应时,您需要一个on_reaction_remove
事件来删除角色:
msg = None
tennis = "\NTABLE TENNIS PADDLE AND BALL"
@client.event
async def on_ready():
channel = client.get_channel('524415641310986252')
global msg
msg = await client.send_message(channel, "React to me!")
await client.add_reaction(msg, tennis)
@client.event
async def on_reaction_add(reaction, user):
if reaction.user == client.user:
return
if reaction.message == msg and reaction.emoji == tennis:
verified = discord.utils.get(user.server.roles, name="verified")
await client.add_roles(user, verified)
@client.event
async def on_reaction_remove(reaction, user):
if reaction.message == msg and reaction.emoji == tennis:
verified = discord.utils.get(user.server.roles, name="verified")
await client.remove_roles(user, verified)
【讨论】:
以上是关于当用户添加反应 Discord.py 时错误赋予角色的主要内容,如果未能解决你的问题,请参考以下文章
.verify 命令未赋予角色(discord.py 1.7.3)
当用户对 2 个反应做出反应时,机器人不会读取第二个反应.. discord.py