2024-12-15 14:59:41 +01:00
|
|
|
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
|
2024-12-15 16:21:01 +01:00
|
|
|
import base64
|
2024-12-15 14:59:41 +01:00
|
|
|
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")
|
2024-12-15 16:03:54 +01:00
|
|
|
bot.db.row_factory = aiosqlite.Row
|
2024-12-15 14:59:41 +01:00
|
|
|
await bot.load_extension("perso_commands")
|
|
|
|
await bot.tree.sync()
|
|
|
|
print(f"Logged in as {bot.user}")
|
|
|
|
|
2024-12-15 15:45:33 +01:00
|
|
|
|
2024-12-15 15:51:13 +01:00
|
|
|
@bot.event
|
2024-12-15 16:27:10 +01:00
|
|
|
async def on_message(message:discord.Message):
|
2024-12-15 15:51:13 +01:00
|
|
|
if message.author.bot:
|
|
|
|
return
|
|
|
|
if message.content.startswith("g!"):
|
2024-12-15 16:03:54 +01:00
|
|
|
if len(message.content.split(" "))<2:
|
|
|
|
return
|
2024-12-15 15:51:13 +01:00
|
|
|
short_name = message.content.split(" ")[0][2:]
|
|
|
|
uuid = message.author.id
|
|
|
|
guild_id = message.guild.id
|
2024-12-15 17:02:05 +01:00
|
|
|
message_str = message.content[message.content.index(" ")+1:]
|
2024-12-15 16:27:10 +01:00
|
|
|
if await say_message(uuid,guild_id,short_name,message_str,message.channel):
|
2024-12-15 17:02:05 +01:00
|
|
|
await message.delete()
|
2024-12-15 15:51:13 +01:00
|
|
|
|
2024-12-15 15:53:28 +01:00
|
|
|
@bot.tree.command(name="s",description="Envoyer un message en tant que personnage")
|
2024-12-15 17:06:01 +01:00
|
|
|
@app_commands.user_install()
|
|
|
|
@app_commands.describe(id="Le nom court du personnage",message="Le message à envoyer")
|
|
|
|
@app_commands.allowed_contexts(guilds=True,dms=True,private_channels=True)
|
2024-12-15 17:06:24 +01:00
|
|
|
@app_commands.allowed_installs(guilds=True,users=True)
|
2024-12-15 15:45:33 +01:00
|
|
|
async def say(interaction:discord.Interaction,id:str,message:str):
|
2024-12-15 17:09:18 +01:00
|
|
|
if not await say_message(interaction.user.id,(interaction.guild.id if interaction.guild is not None else -1),id,message,interaction.channel):
|
2024-12-15 16:14:58 +01:00
|
|
|
print("ok9")
|
2024-12-15 15:51:13 +01:00
|
|
|
await interaction.response.send_message("Vous n'avez pas ce personnage",ephemeral=True)
|
|
|
|
return
|
2024-12-15 16:14:58 +01:00
|
|
|
print("ok10")
|
2024-12-15 15:51:13 +01:00
|
|
|
await interaction.response.send_message("Message envoyé",ephemeral=True)
|
|
|
|
|
|
|
|
|
|
|
|
async def say_message(userid:int, guildid:int, shortname:str, message:str,channel:discord.TextChannel):
|
2024-12-15 16:23:20 +01:00
|
|
|
# print("HELLOWORODL")
|
|
|
|
# print(userid,guildid,shortname,message,channel)
|
2024-12-15 15:55:52 +01:00
|
|
|
res = await bot.db.execute("SELECT * FROM personnages WHERE uuid=? AND (guild_id=? OR guild_id IS NULL) AND nom_court=?",(userid,guildid,shortname))
|
2024-12-15 15:45:33 +01:00
|
|
|
row = await res.fetchone()
|
|
|
|
await res.close()
|
|
|
|
if row is None:
|
2024-12-15 15:51:13 +01:00
|
|
|
return False
|
2024-12-15 16:23:20 +01:00
|
|
|
# print("ok")
|
2024-12-15 16:09:47 +01:00
|
|
|
short_name = row["nom_court"]
|
|
|
|
full_name = row["nom_complet"]
|
2024-12-15 16:49:32 +01:00
|
|
|
perso_image = row["image"]
|
2024-12-15 16:23:20 +01:00
|
|
|
# print(short_name,full_name,perso_image)
|
2024-12-15 15:45:33 +01:00
|
|
|
webhook = None
|
2024-12-15 16:23:20 +01:00
|
|
|
# print("ok2")
|
2024-12-15 17:15:13 +01:00
|
|
|
if channel is discord.channel.DMChannel:
|
2024-12-15 17:13:38 +01:00
|
|
|
await channel.send(f"**{full_name}({short_name})**: {message}")
|
|
|
|
return
|
2024-12-15 15:59:59 +01:00
|
|
|
if discord.utils.get(await channel.webhooks(),name=short_name) is None:
|
2024-12-15 16:23:20 +01:00
|
|
|
# print("ok3");
|
2024-12-15 16:49:32 +01:00
|
|
|
webhook = await channel.create_webhook(name=short_name)
|
2024-12-15 16:23:20 +01:00
|
|
|
# print("ok4")
|
2024-12-15 15:45:33 +01:00
|
|
|
else:
|
2024-12-15 16:23:20 +01:00
|
|
|
# print("ok5")
|
2024-12-15 17:00:50 +01:00
|
|
|
webhook = discord.utils.get(await channel.webhooks(),name=short_name)
|
2024-12-15 16:23:20 +01:00
|
|
|
# print("ok6")
|
|
|
|
# print("ok7")
|
2024-12-15 17:00:50 +01:00
|
|
|
await webhook.send(message,username=full_name,avatar_url=perso_image)
|
2024-12-15 16:23:20 +01:00
|
|
|
# print("ok8")
|
2024-12-15 15:51:13 +01:00
|
|
|
return True
|
|
|
|
|
2024-12-15 14:59:41 +01:00
|
|
|
bot.run(bot_token)
|