Add token validation and implement random voice channel connection in VoiceBot
This commit is contained in:
parent
07613ed4cb
commit
f101a02a99
2 changed files with 35 additions and 4 deletions
botbotbot
|
@ -96,7 +96,11 @@ class ChaosBot:
|
||||||
logger.info(f"We have logged in as {self.bot.user}")
|
logger.info(f"We have logged in as {self.bot.user}")
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
self.bot.run(self.config.get("token"))
|
if not isinstance(tk := self.config.get("token"), str):
|
||||||
|
logger.error("No token.")
|
||||||
|
return
|
||||||
|
|
||||||
|
self.bot.run(tk)
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
@ -27,6 +28,7 @@ class VoiceBot:
|
||||||
|
|
||||||
def init_events(self) -> None:
|
def init_events(self) -> None:
|
||||||
self.bot.add_listener(self.on_voice_state_update, "on_voice_state_update")
|
self.bot.add_listener(self.on_voice_state_update, "on_voice_state_update")
|
||||||
|
self.bot.add_listener(self.random_connect, "on_ready")
|
||||||
self.bot.add_application_command(
|
self.bot.add_application_command(
|
||||||
discord.SlashCommand(
|
discord.SlashCommand(
|
||||||
self.join_voice,
|
self.join_voice,
|
||||||
|
@ -36,14 +38,31 @@ class VoiceBot:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def random_connect(self) -> None:
|
||||||
|
while True:
|
||||||
|
logger.info("Random connect.")
|
||||||
|
for g_id in self.guild_ids:
|
||||||
|
guild = self.bot.get_guild(g_id)
|
||||||
|
if guild is None or random.random() > 10 / 100:
|
||||||
|
continue
|
||||||
|
|
||||||
|
voice_chan = random.choice(guild.voice_channels)
|
||||||
|
await self.connect_voice(voice_chan)
|
||||||
|
await asyncio.sleep(60)
|
||||||
|
|
||||||
async def connect_voice(
|
async def connect_voice(
|
||||||
self, channel: discord.VoiceChannel | discord.StageChannel
|
self, channel: discord.VoiceChannel | discord.StageChannel
|
||||||
) -> None:
|
) -> None:
|
||||||
if self.cambai is None:
|
if self.cambai is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.info("Generating tts")
|
logger.info(f"Connecting to voice channel <{channel}>.")
|
||||||
if len(channel.members) == 1:
|
if len(channel.members) == 0:
|
||||||
|
vo = await self.connect_voice_chan(channel)
|
||||||
|
await asyncio.sleep(10)
|
||||||
|
await vo.disconnect()
|
||||||
|
return
|
||||||
|
elif len(channel.members) == 1:
|
||||||
member = channel.members[0].display_name
|
member = channel.members[0].display_name
|
||||||
else:
|
else:
|
||||||
member = (
|
member = (
|
||||||
|
@ -64,11 +83,19 @@ class VoiceBot:
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
source = await discord.FFmpegOpusAudio.from_probe(self.cambai.tts(script))
|
source = await discord.FFmpegOpusAudio.from_probe(self.cambai.tts(script))
|
||||||
vo: discord.VoiceClient = await channel.connect()
|
vo = await self.connect_voice_chan(channel)
|
||||||
|
|
||||||
await vo.play(source, wait_finish=True)
|
await vo.play(source, wait_finish=True)
|
||||||
await vo.disconnect()
|
await vo.disconnect()
|
||||||
|
|
||||||
|
async def connect_voice_chan(
|
||||||
|
self, channel: discord.VoiceChannel | discord.StageChannel
|
||||||
|
) -> discord.VoiceClient:
|
||||||
|
for vc in self.bot.voice_clients:
|
||||||
|
if isinstance(vc, discord.VoiceClient) and vc.guild == channel.guild:
|
||||||
|
await vc.disconnect()
|
||||||
|
return await channel.connect()
|
||||||
|
|
||||||
async def join_voice(self, ctx: discord.ApplicationContext) -> None:
|
async def join_voice(self, ctx: discord.ApplicationContext) -> None:
|
||||||
if ctx.user.voice:
|
if ctx.user.voice:
|
||||||
await ctx.respond(
|
await ctx.respond(
|
||||||
|
|
Loading…
Add table
Reference in a new issue