Пытаюсь сделать дискорд бота для выдачи роли через пост на питоне
вот код(main.py):
import discord
from discord import utils
import config
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as {0}!'.format(self.user))
async def on_raw_reaction_add(self, payload):
if payload.message_id == config.POST_ID:
channel = self.get_channel(payload.channel_id) # получаем объект канала
message = await channel.fetch_message(payload.message_id) # получаем объект сообщения
member = utils.get(message.guild.members, id=payload.user_id) # получаем объект пользователя который поставил реакцию
try:
emoji = str(payload.emoji) # эмоджик который выбрал юзер
role = utils.get(message.guild.roles, id=config.ROLES[emoji]) # объект выбранной роли (если есть)
if(len([i for i in member.roles if i.id not in config.EXCROLES]) <= config.MAX_ROLES_PER_USER):
await member.add_roles(role)
print('[SUCCESS] User {0.display_name} has been granted with role {1.name}'.format(member, role))
else:
await message.remove_reaction(payload.emoji, member)
print('[ERROR] Too many roles for user {0.display_name}'.format(member))
except KeyError as e:
print('[ERROR] KeyError, no role found for ' + emoji)
except Exception as e:
print(repr(e))
async def on_raw_reaction_remove(self, payload):
channel = self.get_channel(payload.channel_id) # получаем объект канала
message = await channel.fetch_message(payload.message_id) # получаем объект сообщения
member = utils.get(message.guild.members, id=payload.user_id) # получаем объект пользователя который поставил реакцию
try:
emoji = str(payload.emoji) # эмоджик который выбрал юзер
role = utils.get(message.guild.roles, id=config.ROLES[emoji]) # объект выбранной роли (если есть)
await member.remove_roles(role)
print('[SUCCESS] Role {1.name} has been remove for user {0.display_name}'.format(member, role))
except KeyError as e:
print('[ERROR] KeyError, no role found for ' + emoji)
except Exception as e:
print(repr(e))
# RUN
client = MyClient()
client.run(config.TOKEN)
вот код(config.py):
TOKEN = 'токен'
POST_ID = 1010497418577592361
ROLERS = {
'': 1010492903275102219,
'': 1010492014531444738,
'': 1010493035949338644
}
EXCROLES = ()
MAX_ROLERS_PER_USER = 3
Ошибка:
Traceback (most recent call last):
File "C:\Users\egris\Desktop\YouConfig\Дискорд\Бот\main.py", line 51, in <module>
client.run(config.TOKEN)
File "C:\Python_3.9.7\lib\site-packages\discord\client.py", line 598, in run
return future.result()
File "C:\Python_3.9.7\lib\site-packages\discord\client.py", line 579, in runner
await self.start(*args, **kwargs)
File "C:\Python_3.9.7\lib\site-packages\discord\client.py", line 543, in start
await self.connect(reconnect=reconnect)
File "C:\Python_3.9.7\lib\site-packages\discord\client.py", line 457, in connect
await self._connect()
File "C:\Python_3.9.7\lib\site-packages\discord\client.py", line 418, in _connect
self.ws = await asyncio.wait_for(coro, timeout=180.0, loop=self.loop)
File "C:\Python_3.9.7\lib\asyncio\tasks.py", line 481, in wait_for
return fut.result()
File "C:\Python_3.9.7\lib\site-packages\discord\gateway.py", line 244, in from_client
await ws.poll_event()
File "C:\Python_3.9.7\lib\site-packages\discord\gateway.py", line 469, in poll_event
await self.received_message(msg)
File "C:\Python_3.9.7\lib\site-packages\discord\gateway.py", line 385, in received_message
await self.send_as_json(self._keep_alive.get_payload())
File "C:\Python_3.9.7\lib\site-packages\discord\gateway.py", line 484, in send_as_json
await self.send(utils.to_json(data))
File "C:\Python_3.9.7\lib\site-packages\discord\gateway.py", line 480, in send
await super().send(data)
File "C:\Python_3.9.7\lib\site-packages\websockets\protocol.py", line 371, in send
yield from self.write_frame(opcode, data)
File "C:\Python_3.9.7\lib\site-packages\websockets\protocol.py", line 740, in write_frame
with (yield from self._drain_lock):
TypeError: 'Lock' object is not iterable
Пытался сделать так: