Download bots/Enter_content.py from jam134/adad: direct link, hf CLI and curl.
- Browser
- Download file 6.32 kB
-
https://huggingface.co/spaces/jam134/adad/resolve/main/bots/Enter_content.py
- Command line
-
hf download hf://spaces/jam134/adad/bots/Enter_content.py
-
curl -L -o Enter_content.py https://huggingface.co/spaces/jam134/adad/resolve/main/bots/Enter_content.py
6.32 kB
| import discord | |
| from discord.ext import commands | |
| import asyncio | |
| # 設定 intents | |
| intents = discord.Intents.default() | |
| intents.guilds = True # 讓機器人能夠查看伺服器列表 | |
| intents.members = True # 讓機器人能夠讀取伺服器的成員資訊 | |
| intents.message_content = True # 讓機器人能夠讀取訊息內容(這對於某些指令是必要的) | |
| bot = commands.Bot(command_prefix='!', intents=intents) | |
| # 當 bot 準備好時執行 | |
| async def on_ready(): | |
| print(f'Logged in as {bot.user.name}') | |
| print('Bot is ready!') | |
| asyncio.create_task(console_input_loop()) | |
| #開始控制台輸入功能 | |
| async def servermessage(ctx, *, message): | |
| await ctx.send(message) | |
| # 控制台輸入循環 | |
| async def console_input_loop(): | |
| while True: | |
| try: | |
| print("serverlist:列出所有伺服ID\nservermessage:在伺服器中發送內容") | |
| command = await asyncio.get_event_loop().run_in_executor(None, input, "輸入指令: ") | |
| command = command.strip().lower() | |
| if command == 'serverlist': | |
| await list_servers_and_channels() | |
| elif command == 'servermessage': | |
| await send_message() | |
| else: | |
| print("未知的指令。") | |
| except KeyboardInterrupt: | |
| print("\n退出程序。") | |
| await bot.close() | |
| break | |
| async def servers(ctx): | |
| """顯示機器人所在的所有伺服器及其頻道資訊""" | |
| if not bot.guilds: | |
| await ctx.send("機器人目前不在任何伺服器中。") | |
| return | |
| server_info = "" # 初始化 server_info 為空字串 | |
| for index, guild in enumerate(bot.guilds): | |
| # 伺服器名稱和ID | |
| server_info += f"伺服器名稱: {guild.name}, ID: {guild.id}\n" | |
| # 顯示該伺服器的所有頻道 | |
| for index, channel in enumerate(guild.channels): | |
| if isinstance(channel, discord.TextChannel): # 只列出文字頻道 | |
| server_info += f" 頻道名稱: {channel.name}, ID: {channel.id}\n" | |
| # 空行分隔伺服器資料 | |
| server_info += "\n" | |
| if not server_info: | |
| await ctx.send("沒有找到頻道資訊。") | |
| return | |
| # 回傳伺服器及頻道資訊,分成多條訊息發送,避免超過 Discord 訊息長度限制 | |
| for chunk in [server_info[i:i+2000] for i in range(0, len(server_info), 2000)]: | |
| await ctx.send(chunk) | |
| async def list_servers_and_channels(): | |
| print("\n已加入的伺服器:") | |
| for index, guild in enumerate(bot.guilds): | |
| print(f"{index}: 伺服器名稱: {guild.name}, 伺服器 ID: {guild.id}") | |
| try: | |
| guild_index = int(await asyncio.get_event_loop().run_in_executor(None, input, "選擇伺服器 (輸入數字): ")) | |
| if 0 <= guild_index < len(bot.guilds): | |
| guild = bot.guilds[guild_index] | |
| print(f"\n伺服器 {guild.name} 的頻道:") | |
| for index, channel in enumerate(guild.channels): | |
| print(f"{index}: 頻道名稱: {channel.name}, 頻道 ID: {channel.id}") | |
| # 選擇頻道後可以執行其他操作,例如發送訊息 | |
| while True: | |
| action = await asyncio.get_event_loop().run_in_executor(None, input, "輸入操作 (發送訊息: 'send', 結束: 'exit'): ") | |
| action = action.strip().lower() | |
| if action == 'send': | |
| try: | |
| channel_index = int(await asyncio.get_event_loop().run_in_executor(None, input, "選擇頻道 (輸入數字): ")) | |
| if 0 <= channel_index < len(guild.channels): | |
| channel = guild.channels[channel_index] | |
| if isinstance(channel, discord.TextChannel): | |
| message = await asyncio.get_event_loop().run_in_executor(None, input, "輸入要發送的訊息: ") | |
| await channel.send(message) | |
| print(f"訊息已發送到頻道 {channel.name}。") | |
| else: | |
| print("選擇的頻道不是文字頻道。") | |
| else: | |
| print("無效的頻道選擇。") | |
| except ValueError: | |
| print("請輸入有效的數字。") | |
| elif action == 'exit': | |
| break | |
| else: | |
| print("不支援的操作。") | |
| else: | |
| print("無效的伺服器選擇。") | |
| except ValueError: | |
| print("請輸入有效的數字。") | |
| async def send_message(): | |
| print("\n選擇伺服器來發送訊息:") | |
| for index, guild in enumerate(bot.guilds): | |
| print(f"{index}: 伺服器名稱: {guild.name}, 伺服器 ID: {guild.id}") | |
| try: | |
| guild_index = int(await asyncio.get_event_loop().run_in_executor(None, input, "選擇伺服器 (輸入數字): ")) | |
| if 0 <= guild_index < len(bot.guilds): | |
| guild = bot.guilds[guild_index] | |
| print(f"\n伺服器 {guild.name} 的頻道:") | |
| for index, channel in enumerate(guild.channels): | |
| print(f"{index}: 頻道名稱: {channel.name}, 頻道 ID: {channel.id}") | |
| try: | |
| channel_index = int(await asyncio.get_event_loop().run_in_executor(None, input, "選擇頻道 (輸入數字): ")) | |
| if 0 <= channel_index < len(guild.channels): | |
| channel = guild.channels[channel_index] | |
| if isinstance(channel, discord.TextChannel): | |
| message = await asyncio.get_event_loop().run_in_executor(None, input, "輸入要發送的訊息: ") | |
| await channel.send(message) | |
| print(f"訊息已發送到頻道 {channel.name}。") | |
| else: | |
| print("選擇的頻道不是文字頻道。") | |
| else: | |
| print("無效的頻道選擇。") | |
| except ValueError: | |
| print("請輸入有效的數字。") | |
| else: | |
| print("無效的伺服器選擇。") | |
| except ValueError: | |
| print("請輸入有效的數字。") | |