staticky-bot/main.py

71 lines
2.3 KiB
Python

from os import getenv, listdir
import re
import discord
from typing import cast
from discord import app_commands
from discord.app_commands import Choice
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
class MyClient(commands.Bot):
def __init__(self):
intents = discord.Intents.none()
intents.guilds = True
intents.messages = True
intents.message_content = True
super().__init__(intents=intents, command_prefix=".")
self.MY_GUILDS = [discord.Object(id=int(guild_id)) for guild_id in getenv("GUILD_ID").split(',')]
self.initial_extensions = ["cogs." + f[:-3] for f in listdir("./cogs") if
f.endswith(".py") and f.__str__() != "__init__.py"]
# self.add_listener(self.on_message)
async def setup_hook(self):
# cogs
for extension in self.initial_extensions:
await self.load_extension(extension)
for guild in self.MY_GUILDS:
self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild)
async def on_ready(self):
team = cast(discord.Team, self.application.team)
print(f'Logged in as {self.user} (ID: {self.user.id})')
print(f"Discord version : {discord.__version__}")
print(f"Admins : {', '.join([m.name for m in team.members])}")
print('------')
async def on_message(self, msg: discord.Message):
# quoicoubot
quoi = re.compile("[qQ]uoi[ \\.!?;\"»]*$")
if quoi.search(msg.content) is not None:
await msg.reply("Quoicoubeh !")
# je suis
je_suis = "je suis "
if msg.content.lower().startswith(je_suis): # détection
words = msg.content[len(je_suis):].split(' ')
new_pseudo = "" # pseudo creation (do not split a word)
for word in words:
if (len(new_pseudo) + len(word) + 1) > 32:
break
new_pseudo += " " + word
if new_pseudo != "":
member = msg.author
try:
await member.edit(nick=new_pseudo, reason="Il est " + new_pseudo)
except discord.Forbidden:
pass
if __name__ == "__main__":
client = MyClient()
client.run(getenv("TOKEN"))