72 lines
No EOL
2.5 KiB
Python
72 lines
No EOL
2.5 KiB
Python
import discord
|
|
import aiosqlite
|
|
from discord.ext import commands
|
|
from discord import app_commands
|
|
import logging
|
|
import sys
|
|
from public_env import *
|
|
from GammaRPBot_Define import GammaRPBot
|
|
formatter = discord.utils._ColourFormatter()
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
handler.setFormatter(formatter)
|
|
logging.basicConfig(level=logging.INFO, handlers=[handler])
|
|
logger = logging.getLogger()
|
|
|
|
|
|
|
|
intents = discord.Intents.all()
|
|
bot = GammaRPBot(intents)
|
|
|
|
@bot.event
|
|
async def on_ready():
|
|
bot.db = await aiosqlite.connect("main.db")
|
|
bot.db.row_factory = aiosqlite.Row
|
|
await bot.load_extension("perso_commands")
|
|
await bot.tree.sync()
|
|
print(f"Logged in as {bot.user}")
|
|
|
|
|
|
@bot.event
|
|
async def on_message(message):
|
|
if message.author.bot:
|
|
return
|
|
if message.content.startswith("g!"):
|
|
if len(message.content.split(" "))<2:
|
|
return
|
|
short_name = message.content.split(" ")[0][2:]
|
|
uuid = message.author.id
|
|
guild_id = message.guild.id
|
|
message_str = message.content.split(" ")[1]
|
|
await say_message(uuid,guild_id,short_name,message_str,message.channel)
|
|
|
|
@bot.tree.command(name="s",description="Envoyer un message en tant que personnage")
|
|
async def say(interaction:discord.Interaction,id:str,message:str):
|
|
if not await say_message(interaction.user.id,interaction.guild.id,id,message,interaction.channel):
|
|
await interaction.response.send_message("Vous n'avez pas ce personnage",ephemeral=True)
|
|
return
|
|
await interaction.response.send_message("Message envoyé",ephemeral=True)
|
|
|
|
|
|
async def say_message(userid:int, guildid:int, shortname:str, message:str,channel:discord.TextChannel):
|
|
print("HELLOWORODL")
|
|
print(userid,guildid,shortname,message,channel)
|
|
res = await bot.db.execute("SELECT * FROM personnages WHERE uuid=? AND (guild_id=? OR guild_id IS NULL) AND nom_court=?",(userid,guildid,shortname))
|
|
row = await res.fetchone()
|
|
await res.close()
|
|
if row is None:
|
|
return False
|
|
print("ok")
|
|
short_name = row["nom_court"]
|
|
full_name = row["nom_complet"]
|
|
perso_image = row["image"]
|
|
print(short_name,full_name,perso_image)
|
|
webhook = None
|
|
print("ok2")
|
|
if discord.utils.get(await channel.webhooks(),name=short_name) is None:
|
|
webhook = await channel.create_webhook(name=short_name,avatar=perso_image)
|
|
else:
|
|
webhook = discord.utils.get(await channel.webhooks(),name=short_name)
|
|
await webhook.send(message,username=full_name,avatar_url=perso_image)
|
|
return True
|
|
|
|
bot.run(bot_token) |