main.py
import os import sys import json import asyncio import discord from PyQt5 import QtWidgets from discord.ext import commands from asyncqt import QEventLoop, asyncSlot import botDesign with open('./master.json', 'r', encoding='utf-8-sig') as f: data = json.load(f) TOKEN = data["guild"]["TOKEN"] BP = data["guild"]["command_prefix"] intents = discord.Intents.all() bot = commands.Bot(command_prefix=BP, case_insensitive=True, intents=intents) bot.remove_command('help') class ExampleApp(QtWidgets.QMainWindow, botDesign.Ui_MainWindow): def __init__(self, parent=None): super().__init__(parent) self.setupUi(self) self.client = MyDiscordCLient() self.pushBotButton.clicked.connect(self.on_connect_bot) self.pushCogsButton.clicked.connect(self.load_cog) @asyncSlot() async def on_connect_bot(self): await self.client.start(TOKEN) @asyncSlot() async def load_cog(self): for filename in os.listdir("./cogs"): if filename.endswith(".py") and not filename.startswith("_"): bot.load_extension(f"cogs.{filename[:-3]}") print(f'Loaded {filename[:-3]}') class MyDiscordCLient(discord.Client): async def on_ready(self): print('Bot logged discord server.') await self.change_presence(status=discord.Status.online, activity=discord.Game('test')) def main(): app = QtWidgets.QApplication(sys.argv) loop = QEventLoop(app) asyncio.set_event_loop(loop) window = ExampleApp() window.show() loop.run_forever() if __name__ == '__main__': main()
файл в /cogs
import discord import audiopath import audioread from discord.ext import commands from time import sleep class Audio(commands.Cog): def __init__(self, bot): self.bot = bot @commands.Cog.listener() async def on_voice_state_update(self, member: discord.Member, before, after): vc_before = before.channel vc_after = after.channel if vc_before == vc_after: return if vc_before is None and vc_after is not None: channel = member.voice.channel vc = await channel.connect() sleep(.1) vc.play(discord.FFmpegPCMAudio(audiopath.connect)) with audioread.audio_open(audiopath.connect) as f: # Start Playing sleep(f.duration) await vc.disconnect() elif vc_after is None and vc_before is not None: return else: channel = member.voice.channel vc = await channel.connect() sleep(.1) vc.play(discord.FFmpegPCMAudio(audiopath.connect)) with audioread.audio_open(audiopath.connect) as f: # Start Playing sleep(f.duration) await vc.disconnect() def setup(bot): bot.add_cog(Audio(bot))