This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
99 lines (86 loc) · 3.38 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
import asyncio
import json
import locale
import logging
import os
import sys
import traceback
from datetime import datetime
import asyncpg
import discord
from discord.ext import commands
import config
async def run():
locale.setlocale(locale.LC_TIME, "es_CL")
logging.basicConfig(
filename="cocoloca.log",
filemode="w",
level=logging.INFO,
format="%(asctime)s:%(levelname)s:%(message)s",
datefmt="%d-%m-%Y %H:%M:%S",
)
db = await asyncpg.create_pool(**config.credentials)
bot = Bot(db=db)
try:
await bot.start(config.discord_token)
except KeyboardInterrupt:
await bot.logout()
class Bot(commands.Bot):
def __init__(self, **kwargs):
super().__init__(command_prefix=config.prefix, case_insersitive=True)
self.db = kwargs.pop("db")
# Cargar extensiones
for extension in os.listdir("./cogs"):
if extension.endswith(".py"):
cog = f"cogs.{extension[:-3]}"
try:
self.load_extension(cog)
except Exception as e:
logging.error(e)
async def on_ready(self):
# Emojis
self.think = self.get_emoji(702729662413013102)
self.omg = self.get_emoji(702729661997645834)
self.love = self.get_emoji(702729661385539667)
# Cargar informacion de aldeanos
with open("data/villagers.json", encoding="utf-8") as data:
self.villagers = json.load(data)
# Uptime
if not hasattr(self, "uptime"):
self.uptime = datetime.now()
info = f"Ready: {self.user} ID: {self.user.id} Guilds: {len(self.guilds)}"
logging.info(info)
async def on_message(self, message):
# Ignorar bots y DMs
if message.author.bot or not message.guild:
return
await self.process_commands(message)
async def on_command_error(self, ctx, error):
name, hint = ctx.author.name, None
if isinstance(error, commands.CommandOnCooldown):
hint = f"No tan rapido {name}, Intenta de nuevo en {error.retry_after:.1f} segundos."
elif isinstance(error, commands.MaxConcurrencyReached):
hint = f"Si quieres ver una lista nueva, cierra la lista anterior con ⏹."
elif isinstance(error, commands.MissingAnyRole):
hint = f"Disculpa {name}, este comando valido solo para nuestros Isleñes."
elif isinstance(error, commands.MissingPermissions):
hint = f"No tienes permisos para usar este comando {name}."
elif isinstance(error, commands.BadArgument):
hint = "Uno de tus parametros no es valido."
elif isinstance(error, commands.MissingRequiredArgument):
hint = f"Te falta un parametro obligatorio {name}."
if hint:
await ctx.send(hint, delete_after=10)
else:
logging.error(error)
if isinstance(error, commands.CommandInvokeError):
text = f"In {ctx.command.qualified_name}: {error.original.__class__.__name__}: {error.original}\n"
etype = type(error)
trace = error.__traceback__
verbosity = 4
lines = traceback.format_exception(etype, error, trace, verbosity)
text += "".join(lines)
logging.error(text)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(run())