DiscordBot / bot_main.py
MKE0108's picture
Update bot_main.py
38394f5 verified
raw
history blame
3.78 kB
import discord
from discord.ext import commands
from discord import app_commands
import difflib
import os
try:
from keys import init
init.set_env()
except:
pass
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.command()
async def sync(ctx):
try:
commands=await bot.tree.sync()
print("同步完成",len(commands))
await ctx.send("同步完成")
except Exception as e:
print(e)
@bot.event
async def on_ready():
print(f'{bot.user} 已連線到 Discord!')
@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 copy
import random
def get_pick_embed(player_source):
player=player_source.copy()
if(len(player) > 5):
# 紅色顯示
return discord.Embed(title="抽獎結果", color=0xff0000, description="人數超過5人")
lane=["🗡️ 上路","💰 打野","🔮 中路","🏹 下路","🚑 輔助"]
while(len(player) < 5):
player.append("---")
random.shuffle(player)
embed = discord.Embed(title="抽獎結果", color=0x00ff00)
for i in range(len(player)):
embed.add_field(value=player[i], name=lane[i], inline=False)
return embed
@bot.hybrid_command(name="選路",description="會從<<指定語音頻道>>和<<指定其他玩家名稱>>中選路線")
@app_commands.describe(channel_name="語音頻道名稱",other_user="其他玩家名稱")
async def pick(ctx, channel_name:str="",other_user:str=""):
#尋找最接近的語音頻道
voice_channel_list = [item.name.upper() for item in ctx.guild.voice_channels]
text_to_match = channel_name.upper()
closest_match = difflib.get_close_matches(text_to_match, voice_channel_list, n=1, cutoff=0.2)
closest_match = closest_match[0] if closest_match else None
# 搜尋指定名稱的語音頻道
try:
idx=voice_channel_list.index(closest_match)
channel_name=ctx.guild.voice_channels[idx].name
voice_channel = discord.utils.get(ctx.guild.voice_channels, name=channel_name)
members = voice_channel.members
member_names = [f"<@{member.id}>" for member in members]
except:
member_names = []
if(other_user != ""):
member_names.extend(other_user.split(" "))
if(len(member_names) == 0):
await ctx.send("沒東東")
return
embed = get_pick_embed(member_names)
solgan = ["我從不覺得在召喚峽谷快樂過","一但加入了召喚峽谷就再也回不去了","我們的遊戲就是要讓你們不開心","你們的不開心就是我們的快樂","你們滿腦子只想著自己","還真是虛情假義呢"]
remessage = f"## {random.choice(solgan)}\n"
tmp=channel_name if channel_name!="" else '沒有指定'
remessage += f"### 🎙️ {tmp}"
tmp=" ".join(member_names)
remessage += f" 👥 {tmp}"
await ctx.send(remessage,embed=embed,delete_after=100)
@bot.hybrid_command(name="一起聽",description="加入sync-tube")
async def join(ctx):
await ctx.send(f"https://sync-tube.de/room/mIzZnHv8", delete_after=100)
bot.run(os.getenv('BOT_TOKENS'))