Migration vers Disnake et ajout des components V2

This commit is contained in:
Mathis 2026-05-09 18:42:42 +02:00
parent c8c579eefe
commit 98f7501e07
47 changed files with 1196 additions and 722 deletions

View file

@ -0,0 +1,30 @@
import disnake
from disnake.ui import View, Button
import asyncio
class MyView(View):
def __init__(self):
super().__init__(timeout=None)
@disnake.ui.button(label="Test")
async def my_btn(self, arg1, arg2):
print(f"Arg1 type: {type(arg1)}")
print(f"Arg2 type: {type(arg2)}")
async def main():
view = MyView()
btn = view.children[0]
print(f"Button: {btn}")
# Mock an interaction
class MockInteraction:
pass
try:
await btn.callback(MockInteraction())
except Exception as e:
print(f"Error during callback: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(main())

View file

@ -0,0 +1,4 @@
import disnake
import disnake.ext.commands as commands
bot = commands.Bot(command_prefix="!")
print(f"Check methods: {[m for m in dir(bot) if 'check' in m]}")

View file

@ -0,0 +1,16 @@
import sqlite3
import os
db_path = "config.db"
if os.path.exists(db_path):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
print(f"Tables: {tables}")
for table in tables:
cursor.execute(f"PRAGMA table_info({table[0]});")
print(f"Schema for {table[0]}: {cursor.fetchall()}")
conn.close()
else:
print("Database not found.")

View file

@ -0,0 +1,19 @@
import disnake
import disnake.ext.commands as commands
import asyncio
print(f"Disnake version: {disnake.__version__}")
bot = commands.Bot(command_prefix="!")
print(f"Bot has setup_hook: {hasattr(bot, 'setup_hook')}")
async def test():
print("Testing setup_hook call...")
class TestBot(commands.Bot):
async def setup_hook(self):
print("setup_hook CALLED!")
tbot = TestBot(command_prefix="!")
# We won't start it because we don't have a token here,
# but we can check if the library expects it.
asyncio.run(test())

View file

@ -0,0 +1,7 @@
import disnake
import disnake.ext.commands as commands
import asyncio
bot = commands.Bot(command_prefix="!")
print(f"Bot methods: {[m for m in dir(bot) if 'setup' in m or 'load' in m]}")
print(f"Is context manager: {hasattr(bot, '__aenter__')}")

View file

@ -0,0 +1,6 @@
import disnake
import disnake.ext.commands as commands
import inspect
bot = commands.Bot(command_prefix="!")
print(f"load_extension is coroutine: {inspect.iscoroutinefunction(bot.load_extension)}")

View file

@ -0,0 +1,8 @@
import disnake
print(f"disnake.TextStyle: {hasattr(disnake, 'TextStyle')}")
print(f"disnake.TextInputStyle: {hasattr(disnake, 'TextInputStyle')}")
try:
import disnake.ui
print(f"disnake.ui.TextInputStyle: {hasattr(disnake.ui, 'TextInputStyle')}")
except:
pass

View file

@ -0,0 +1,52 @@
import os
import re
def fix_file(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Split by class to be surgical
classes = re.split(r'^(class\s+.*)$', content, flags=re.MULTILINE)
new_content = []
if len(classes) > 0:
new_content.append(classes[0]) # Header
for i in range(1, len(classes), 2):
class_header = classes[i]
class_body = classes[i+1] if i+1 < len(classes) else ""
# Determine if it's a View or a Modal
is_view = "disnake.ui.View" in class_header or "(disnake.ui.View)" in class_header or "super().__init__(timeout=" in class_body
is_modal = "disnake.ui.Modal" in class_header or "(disnake.ui.Modal)" in class_header or "super().__init__(title=" in class_body
if is_view:
# Views use add_item
class_body = class_body.replace("self.append_component(", "self.add_item(")
# Ensure super().__init__ doesn't have components=[] if it's a view
class_body = class_body.replace("super().__init__(components=[],", "super().__init__(")
class_body = class_body.replace("super().__init__(components=[], ", "super().__init__(")
if is_modal:
# Modals use append_component
class_body = class_body.replace("self.add_item(", "self.append_component(")
# Modals MUST have components=[] in super().__init__ if not already there
# (My previous sed might have added it correctly)
pass
new_content.append(class_header)
new_content.append(class_body)
final_content = "".join(new_content)
if final_content != content:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(final_content)
return True
return False
for root, dirs, files in os.walk('commandes'):
for file in files:
if file.endswith('.py'):
path = os.path.join(root, file)
if fix_file(path):
print(f"Fixed {path}")

View file

@ -0,0 +1,36 @@
import os
import re
def fix_text_input_default(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Match TextInput( followed by anything including newlines, until )
# This is a bit risky but we can refine it.
# We look for default= inside the arguments of TextInput
def replace_default(match):
args = match.group(2)
new_args = args.replace("default=", "value=")
return f"{match.group(1)}{new_args})"
# Regex: (disnake\.ui\.TextInput\(|ui\.TextInput\(|TextInput\() (.*? \))
# We need to handle nested parentheses if any, but TextInput usually doesn't have them in args.
new_content = re.sub(r'((?:disnake\.ui\.|ui\.|)TextInput\()(.*?\))', replace_default, content, flags=re.DOTALL)
if new_content != content:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(new_content)
return True
return False
files_to_check = [
"commandes/moderation.py",
"commandes/ticket/__init__.py"
]
for path in files_to_check:
if os.path.exists(path):
if fix_text_input_default(path):
print(f"Fixed {path}")

View file

@ -0,0 +1,7 @@
import disnake
import inspect
try:
print(f"Modal init signature: {inspect.signature(disnake.ui.Modal.__init__)}")
except Exception as e:
print(f"Error inspecting Modal: {e}")

View file

@ -0,0 +1,7 @@
import disnake
import inspect
try:
print(f"Section init signature: {inspect.signature(disnake.ui.Section.__init__)}")
except Exception as e:
print(f"Error inspecting Section: {e}")

View file

@ -0,0 +1,3 @@
import disnake
print(disnake.ui.Container)
print(disnake.ui.TextDisplay)

View file

@ -0,0 +1,17 @@
import disnake
import asyncio
async def main():
class DummyResponse:
async def edit_message(self, *args, **kwargs):
print(kwargs)
inter = type('DummyInter', (), {'response': DummyResponse()})()
container = disnake.ui.Container()
try:
await inter.response.edit_message(components=[container])
print("Success")
except Exception as e:
print(f"Error: {e}")
asyncio.run(main())