import asyncio
import os
import datetime
import discord
from discord import channel
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext, cog_ext
from discord_slash.utils.manage_commands import create_option, create_choice
from core.classes import CogExtension


class System(CogExtension):

    @cog_ext.cog_slash(  # ping: 偵測延遲毫秒
        name="ping", description="監測 Bot 和 Discord 之間的連線延遲。")
    async def ping(self, ctx: SlashContext):
        embed = discord.Embed(color=0x21e828, timestamp=datetime.datetime.utcnow())
        embed.set_author(name="MinatoBot",
                        icon_url="https://i.imgur.com/EvyjjO9.jpg")
        embed.add_field(name="Ping", value=f"**{round(self.bot.latency*1000)}** ms", inline=False)
        await ctx.send(embed=embed)

    @commands.command()  # bc: 指定頻道廣播
    async def bc1(self, ctx, ch: int, *, msg):
        channel = self.bot.get_channel(ch)
        await channel.send(f"{msg}")
        await ctx.send(f"在 {channel.mention} 廣播訊息成功。")

    @cog_ext.cog_slash(
        name="bc",
        description="在指定頻道進行廣播。",
        options=[
            create_option(
                name="channel",
                description="傳送訊息的頻道。",
                option_type=7,
                required=True
            ),
            create_option(
                name="message",
                description="訊息。",
                option_type=3,
                required=True
            )
        ]
    )
    async def bc(self, ctx: SlashContext, channel, message):
        await channel.send(f"{message}")
        await ctx.send(f"在 {channel.mention} 廣播訊息成功。")

    @commands.Cog.listener()  # こんあくあ～！
    async def on_message(self, msg):
        if msg.content.startswith("こんあくあ") and msg.author != self.bot.user:
            await msg.channel.send("こんあくあ～！")
    

def setup(bot):
    bot.add_cog(System(bot))
