M
M
MISHAN2020-12-25 17:25:44
Python
MISHAN, 2020-12-25 17:25:44

discord.py error?

I'm wrong friends!
It's called like this:

C:\Discord Bot>python bot.py
File "bot.py", line 51
client.run(config.TOKEN)import


discord I understand in Python, I just wanted to make a bot in 3 seconds, and then an error ((

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MISHAN, 2020-12-26
@ohhmannn

Hello people , I solved this kind of problem ... I'm very ashamed that I did not pay attention to what I copied and ran here ... Special
thanks to @Alexandre888 and @shurshur , for making me think about the task, I'm just like got up this morning, had breakfast and sat down to sit and fix the code.
Briefly about the error! The problem was often due to these two lines of code - and Solution: In the line -
client.run(config.TOKEN)import discord
client.run(config.TOKEN)

<code lang="python">
client.run(config.TOKEN)import discord
</code> , после (config.TOKEN) стоит import discord - этот import discord нужно перенести на другую строку .
Итог такой 
<code lang="python">
client = MyClient()
client.run('config.TOKEN')
import discord
from discord import utils
</code>

А в строке <code lang="python">
client.run(config.TOKEN)
</code> добовляете ' !
<b>Итог :</b>
<code lang="python">
client.run('config.TOKEN')
</code>



<b>Надеюсь я кому то помог с этим максимально простым кодом , и с его максимально глупой ошибкой в исходном коде ! Учите python .</b>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question