DiscordBot / bot_main.py
MKE0108's picture
add
caff5b3
raw
history blame
2.7 kB
import discord
from discord.ext import commands
import os
if(os.getenv('BOT_TOKENS') == None):
from keys import init
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'{bot.user} 已連線到 Discord!')
@bot.command()
async def synccommands(ctx):
await bot.tree.sync()
await ctx.send('同步完成')
@bot.hybrid_command(description="這個指令會回覆你的問候")
async def hello(ctx):
await ctx.send(f'你好 {ctx.author.name}!')
@bot.hybrid_command(description="測試延遲")
async def ping(ctx):
await ctx.send(f'Pong! 延遲: {round(bot.latency * 1000)}ms')
@bot.hybrid_command(description="顯示機器人資訊")
async def info(ctx):
embed = discord.Embed(title="機器人資訊", color=0x00ff00)
embed.add_field(name="名稱", value=bot.user.name, inline=True)
embed.add_field(name="ID", value=bot.user.id, inline=True)
embed.add_field(name="伺服器數量", value=len(bot.guilds), inline=True)
await ctx.send(embed=embed)
import random
def get_pick_embed(player):
if(len(player) > 5):
# 紅色顯示
return discord.Embed(title="抽獎結果", color=0xff0000, description="人數超過5人")
random.shuffle(player)
lane=["🗡️ 上路","💰 打野","🔮 中路","🏹 下路","🚑 輔助"]
while(len(player) < 5):
player.append("---")
embed = discord.Embed(title="抽獎結果", color=0x00ff00)
for i in range(len(player)):
embed.add_field(value=player[i], name=lane[i], inline=False)
solgan = ["我從不覺得在召喚峽谷快樂過","一但加入了召喚峽谷就再也回不去了","我們的遊戲就是要讓你們不開心","你們的不開心就是我們的快樂","你們滿腦子只想著自己","還真是虛情假義呢"]
return embed
@bot.hybrid_command(description="抽路線指令, 用法: !pick 語音聊天室名稱 人名1 人名3 ...")
async def pick(ctx, channel_name,other_user=""):
# 搜尋指定名稱的語音頻道
voice_channel = discord.utils.get(ctx.guild.voice_channels, name=channel_name)
if voice_channel:
# 獲取頻道中的成員
members = voice_channel.members
if members:
member_names = [member.name for member in members]+other_user.split(" ")
embed = get_pick_embed(member_names)
await ctx.send(embed=embed)
else:
await ctx.send(f"{channel_name} 頻道中目前沒有成員")
else:
await ctx.send(f"找不到名為 {channel_name} 的語音頻道")
bot.run(os.getenv('BOT_TOKENS'))