-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
156 lines (123 loc) · 5.25 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import datetime
from rules import WELCOME, ABOUT, COMMANDS, ADMS
from discord.ext import commands
from dotenv import load_dotenv
import databaser
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
bot = commands.Bot(command_prefix='/')
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
@bot.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(WELCOME.format(member.name))
@bot.command(name='regras', help='Envia as regras do servidor no privado do usuário')
async def regras(ctx):
await ctx.author.create_dm()
await ctx.author.dm_channel.send(WELCOME.format(ctx.author.name))
@bot.command(name='adms', help='Informações sobre os administradores do servidor')
async def adms(ctx):
await ctx.author.create_dm()
await ctx.author.dm_channel.send(ADMS)
@bot.command(name='sobre', help='Informações sobre o servidor')
async def sobre(ctx):
await ctx.author.create_dm()
await ctx.author.dm_channel.send(ABOUT)
@bot.command(name='ajuda', help='Informações sobre os comandos do DevAllBot')
async def ajuda(ctx):
await ctx.author.create_dm()
response = 'Comandos registrados do sistema: \n'
for command in COMMANDS:
response += command+'\n'
await ctx.author.dm_channel.send(response)
@bot.command(name='canal', help='Comando para enviar mensagem a todos os membros do servidor')
@commands.has_role('adm')
async def canal(ctx, message):
# Define a resposta como tudo a partir do '/canal '
response = message
# Percorre a lista de servidores ao qual o bot faz part, e encotra a que está no arquivo .env
guild = None
for g in bot.guilds:
if g.name == GUILD:
guild = g
break
# Percorre a lista de membros do servidor
for member in guild.members:
# Ignora o bot na lista de usuários
if member != bot.user:
# Tenta enviar uma mensagem privada
try:
await member.create_dm()
await member.dm_channel.send(response)
except:
print(member.name)
@bot.command(name='poll')
async def poll(ctx, poll_name, option=None):
databaser.cursor.execute(f"select titulo from votacoes where titulo='{poll_name}'")
titulo_votacao = databaser.cursor.fetchone()
if titulo_votacao is not None:
# Verifica se possui uma opção para voto
if option is not None:
# Testando se a opção do voto existe
databaser.cursor.execute(f"select opcao from alternativas where votacao='{poll_name}' and opcao='{option}'")
opcao_votacao = databaser.cursor.fetchone()
if opcao_votacao is not None:
# Registrando voto
databaser.cursor.execute(f"insert into votos(nome, opcao, votacao) "
f"values ('{ctx.author.name}','{option}','{poll_name}')")
databaser.conn.commit()
await ctx.send("Voto computado com sucesso ;)")
# Verifica se foi passado alguma opção de voto
else:
response = f"Votação {poll_name}\n"
# Consulta as informações sobre as opções
databaser.cursor.execute(f"select opcao from alternativas where votacao='{poll_name}'")
opcoes = databaser.cursor.fetchall()
for opcao in opcoes:
databaser.cursor.execute(f"select count(*) from votos where votacao='{poll_name}' and opcao='{opcao[0]}'")
total = databaser.cursor.fetchone()
response += f"{opcao[0]} - {total[0]}\n"
await ctx.send(response)
else:
await ctx.send("Não há nenhuma votação com o título informado :/")
@bot.command(name='new-poll')
@commands.has_role('adm')
async def new_poll(ctx, poll_name, *options):
# Registrando pergunta
databaser.cursor.execute(f"insert into votacoes(titulo) values ('{poll_name}')")
databaser.conn.commit()
for opcao in options:
# Registrando opções
databaser.cursor.execute(f"insert into alternativas(opcao, votacao) "
f"values ('{opcao}', '{poll_name}')")
databaser.conn.commit()
await ctx.send("Votação registrada com sucesso")
@bot.command(name='polls')
async def polls(ctx):
databaser.cursor.execute(f"select titulo from votacoes")
titulos = databaser.cursor.fetchall()
print(titulos)
response = "Votações ativas\n"
for titulo in titulos:
response += '- ' + titulo[0] + '\n'
await ctx.send(response)
@bot.command(name='cronograma')
async def cronograma(ctx):
response = "Cronograma\n"
databaser.cursor.execute("SELECT titulo, orador, data FROM cronogramas")
cronogramas = databaser.cursor.fetchall()
for row in cronogramas:
data = datetime.datetime.strptime(row[2], '%Y-%m-%d').date()
data = data.strftime('%d/%m/%Y')
response += f'- {row[0]} - {row[1]} - {data}\n'
await ctx.author.create_dm()
await ctx.author.dm_channel.send(response)
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.errors.CheckFailure):
await ctx.send('Você não têm permissão para usar esse comando :/')
bot.run(TOKEN)