Skip to content

Instantly share code, notes, and snippets.

@pedroricardo
Last active July 30, 2022 07:30
Show Gist options
  • Save pedroricardo/8db25e8503dbcdbd35a73b48baf556de to your computer and use it in GitHub Desktop.
Save pedroricardo/8db25e8503dbcdbd35a73b48baf556de to your computer and use it in GitHub Desktop.
Criando Bot Para Discord #4 - Adicionar Cargos por Reações [Python] [PT-BR]
import discord
import secreto
import asyncio
client = discord.Client()
COR =0x690FC3
token = secreto.seu_token()
msg_id = None
msg_user = None
@client.event
async def on_ready():
print('BOT ONLINE - Olá Mundo!')
print(client.user.name)
print(client.user.id)
print('--------PR-------')
@client.event
async def on_message(message):
if message.content.lower().startswith("!lol"):
embed1 =discord.Embed(
title="Escolha seu Elo!",
color=COR,
description="- Bronze = 🐤\n"
"- Prata = 📘 \n"
"- Ouro = 📙",)
botmsg = await client.send_message(message.channel, embed=embed1)
await client.add_reaction(botmsg, "🐤")
await client.add_reaction(botmsg, "📘")
await client.add_reaction(botmsg, "📙")
global msg_id
msg_id = botmsg.id
global msg_user
msg_user = message.author
@client.event
async def on_reaction_add(reaction, user):
msg = reaction.message
if reaction.emoji == "🐤" and msg.id == msg_id: #and user == msg_user:
role = discord.utils.find(lambda r: r.name == "Bronze", msg.server.roles)
await client.add_roles(user, role)
print("add")
if reaction.emoji == "📘" and msg.id == msg_id: #and user == msg_user:
role = discord.utils.find(lambda r: r.name == "Prata", msg.server.roles)
await client.add_roles(user, role)
print("add")
if reaction.emoji == "📙" and msg.id == msg_id: #and user == msg_user:
role = discord.utils.find(lambda r: r.name == "Ouro", msg.server.roles)
await client.add_roles(user, role)
print("add")
@client.event
async def on_reaction_remove(reaction, user):
msg = reaction.message
if reaction.emoji == "🐤" and msg.id == msg_id: #and user == msg_user:
role = discord.utils.find(lambda r: r.name == "Bronze", msg.server.roles)
await client.remove_roles(user, role)
print("remove")
if reaction.emoji == "📘" and msg.id == msg_id: #and user == msg_user:
role = discord.utils.find(lambda r: r.name == "Prata", msg.server.roles)
await client.remove_roles(user, role)
print("remove")
if reaction.emoji == "📙" and msg.id == msg_id: #and user == msg_user:
role = discord.utils.find(lambda r: r.name == "Ouro", msg.server.roles)
await client.remove_roles(user, role)
print("remove")
client.run(token)
def seu_token():
return "xxxxxxxx"
#Subistitua xxxxxx pelo seu token!!
@ZeroOne-alt
Copy link

ZeroOne-alt commented Feb 25, 2020

o meu comando esta dando problema na parte do client.send_message(message.channel, embed = embed1)

da o erro: AttributeError: 'Client' object has no attribute 'send_message'

e dando erro na pasta .py do client

erro do client: line 312, in _run_event
await coro(*args, **kwargs)

poderia me ajudar ?

n, consegui encontrar solução para o problema

@LeWal12
Copy link

LeWal12 commented May 31, 2020

o meu comando esta dando problema na parte do client.send_message(message.channel, embed = embed1)

da o erro: AttributeError: 'Client' object has no attribute 'send_message'

e dando erro na pasta .py do client

erro do client: line 312, in _run_event
await coro(*args, **kwargs)

poderia me ajudar ?

n, consegui encontrar solução para o problema

A API atualizou. Eu coloquei assim:
botmsg = await message.channel.send(embed = embed)

@giovaneferreira7
Copy link

Muito top, professor muito bom.

@Victors3ixas
Copy link

Então, com o passar do tempo, acredito que tenha mudado muita coisa, para tanto, quando eu executo a segunda parte, de remover a reação, simplesmente não acontece nada.

@client.event
async def on_reaction_remove(reaction, user):
    print('estou funcionando') 
    msg = reaction.message

if reaction.emoji == "💠" and msg.id == msg_id:
    role = discord.utils.find(lambda r: r.id == 822994803046088715, msg.guild.roles)
    await user.remove_roles(role)
    print(f'tag {role.name} removida.')

eu ainda tentei colocar um print abaixo da função on_reaction_remove para ter certeza se pelo menos até aquele ponto ela funcionava, mas nem isso, o que me leva à acreditar que há algum problema na função, tendo vista que ela não executa nem mesmo uma linha abaixo dela.

pesquisei varias coisas, coloquei raw, e mais outras coisas, tentei tudo o que podia, mas nada funciona, ja to ficando perdido.

@AliceBraga7
Copy link

AliceBraga7 commented May 23, 2021

o código tem vários erros. até tentei arrumar, mas sou leiga na área e acabei embolando tudo. de todos os outros códigos, esse foi o mais fácil de entender a lógica, mas não quer dizer que foi o mais prático.

`import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!") #definir o prefixo
COR= 0xf109e9 #cor em html

@bot.event # bot entrar online
async def on_ready():
print("BOT ONLINE - Vam bora sô!")
print(bot.user.name)
print(bot.user.id)
print("-----------pp-----------")

@bot.command() # definimos o comando
async def ola(ctx): # aqui definimos o comando e quando digitarmos "!ola" o bot vai responder
msg = 'olá mundo' # criamos uma váriavel para mandar a msg
await ctx.send(msg)

@bot.command()
async def embed(ctx): #criando o embed, o nome do comando será !embed
embed = discord.Embedembed=discord.Embed(
title="Escolha seu Elo!",
color=COR,
description="- Bronze = 🐤\n"
"- Prata = 📘 \n"
"- Ouro = 📙",)

await ctx.send(embed=embed)`

@Joanliver
Copy link

Gostaria de marcar um cargo fora do embed so que no mesmo comando tem alguma dica ?

@MateusSlovakSilva
Copy link

quando clico no emoji não esta add o cargo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment