| |
| from dns_resolver import dns_manager |
| |
| |
| dns_manager.patch_socket() |
|
|
| import os |
| import discord |
| import time |
| from fastapi import FastAPI |
| import uvicorn |
| import asyncio |
| from threading import Thread |
| from discord import app_commands, ui |
| |
| from mongo import MongoManager |
|
|
|
|
| |
| db_manager = MongoManager(os.getenv("MONGO_URL"), "Main") |
|
|
| |
| |
|
|
| |
| app = FastAPI() |
| |
| @app.api_route("/", methods=["GET", "HEAD"]) |
| async def status(): |
| |
| return {"status": "hi"} |
|
|
| def run_flask(): |
| |
| port = int(os.environ.get('PORT', 7860)) |
| uvicorn.run(app, host='0.0.0.0', port=port) |
|
|
| |
| def format_pings(role_input: str): |
| pass |
|
|
| |
| class AnnounceModal(ui.Modal): |
| def __init__(self, channel: discord.TextChannel, ping_roles_str: str = None): |
| super().__init__(title="New Announcement") |
| self.channel = channel |
| self.ping_roles_str = ping_roles_str |
|
|
| announcement_title = ui.TextInput(label="Title", placeholder="Enter title...", style=discord.TextStyle.short) |
| announcement_body = ui.TextInput(label="Body", placeholder="Enter message...", style=discord.TextStyle.long) |
|
|
| async def on_submit(self, interaction: discord.Interaction): |
| content = self.announcement_body.value |
| pings = self.ping_roles_str |
| if pings: pass |
| |
| embed = discord.Embed( |
| title=self.announcement_title.value, |
| description=content, |
| color=discord.Color.red() |
| ) |
| footer = f"\n\n||{pings}||\n\n---\n**Sent by:** ||{interaction.user.mention}||\n**Time:** <t:{int(time.time())}:f>" |
| embed.description += footer |
|
|
| await self.channel.send(embed=embed) |
| await interaction.response.send_message(f"✅ Sent to {self.channel.mention}!", ephemeral=False) |
|
|
| class EditAnnounceModal(ui.Modal): |
| def __init__(self, message: discord.Message, new_ping_roles_str: str = None): |
| super().__init__(title="Edit Announcement") |
| self.message = message |
| self.new_ping_roles_str = new_ping_roles_str |
| |
| old_title = message.embeds[0].title if message.embeds else "" |
| old_body = message.embeds[0].description.split("\n\n---")[0] if message.embeds else "" |
|
|
| self.edit_title = ui.TextInput(label="Title", default=old_title, style=discord.TextStyle.short) |
| self.edit_body = ui.TextInput(label="Body", default=old_body, style=discord.TextStyle.long) |
| |
| self.add_item(self.edit_title) |
| self.add_item(self.edit_body) |
|
|
| async def on_submit(self, interaction: discord.Interaction): |
| content = self.edit_body.value |
| pings = self.new_ping_roles_str |
| if pings: pass |
|
|
| new_embed = discord.Embed( |
| title=self.edit_title.value, |
| description=content, |
| color=discord.Color.blue() |
| ) |
| footer = f"\n\n||{pings}||\n\n---\n**Edited by:** ||{interaction.user.mention}||\n**Time:** <t:{int(time.time())}:f>" |
| new_embed.description += footer |
|
|
| await self.message.edit(embed=new_embed) |
| await interaction.response.send_message(f"✅ Updated announcement {self.message.id} !", ephemeral=False) |
|
|
| |
| class MyBot(discord.Client): |
| def __init__(self): |
| super().__init__(intents=discord.Intents.default()) |
| self.tree = app_commands.CommandTree(self) |
|
|
| async def setup_hook(self): |
| await self.tree.sync() |
| print(f"✅ Slash commands synced") |
|
|
| client = MyBot() |
| client.db = db_manager |
|
|
| |
| @client.tree.command(name="announce", description="Post announcement") |
| async def announce(interaction: discord.Interaction, channel: discord.TextChannel, role_ids: str = None): |
| |
| |
| |
| await interaction.response.send_modal(AnnounceModal(channel, role_ids)) |
|
|
| @client.tree.command(name="announce_edit", description="Edit announcement") |
| async def announce_edit(interaction: discord.Interaction, channel: discord.TextChannel, message_id: str, new_role_ids: str = None): |
| |
| |
| |
| try: |
| message = await channel.fetch_message(int(message_id)) |
| await interaction.response.send_modal(EditAnnounceModal(message, new_role_ids)) |
| except Exception as e: |
| await interaction.response.send_message(f"❌ Error: {e}", ephemeral=False) |
|
|
| @client.tree.command(name="commands", description="Show all avaliable commands") |
| async def wismer_commands(interaction: discord.Interaction): |
| await interaction.response.send_message( |
| '''**Avaliable Commands**: |
| `/announce`: Post an announcement. |
| `/announce_edit`: Edit an announcement.''', |
| ephemeral=False |
| ) |
|
|
| |
|
|
| |
| ALLOWED_USERS = ["tonydong365"] |
|
|
| |
| async def send_anonymous_message(channel, content): |
| await channel.send(content) |
|
|
| @client.tree.command(name="say", description="idk what is this") |
| @app_commands.describe(content="idk") |
| async def secret(interaction: discord.Interaction, content: str): |
| |
| current_user = interaction.user.name |
| |
| |
| if current_user not in ALLOWED_USERS: |
| |
| |
| await interaction.response.send_message("This is nothing.", ephemeral=True) |
| return |
|
|
| |
| |
| await interaction.response.send_message("指令已确认...", ephemeral=True) |
| |
| |
| await interaction.delete_original_response() |
|
|
| |
| await send_anonymous_message(interaction.channel, content) |
|
|
|
|
|
|
| @client.tree.command(name="hi") |
| async def hi(interaction: discord.Interaction): |
| await interaction.response.defer(ephemeral=False) |
| await interaction.followup.send("hi!") |
|
|
|
|
| |
| async def start_bot(): |
| token = os.getenv('DISCORD_TOKEN') |
| while True: |
| try: |
| print("正在尝试连接 Discord...") |
| await client.start(token) |
| except Exception as e: |
| print(f"❌ 连接失败: {e},15秒后重试...") |
| |
| try: |
| await client.close() |
| except: |
| pass |
| await asyncio.sleep(15) |
|
|
| if __name__ == "__main__": |
| |
| t = Thread(target=run_flask) |
| t.daemon = True |
| t.start() |
| |
| |
| try: |
| asyncio.run(start_bot()) |
| except KeyboardInterrupt: |
| pass |
|
|