-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
87 lines (62 loc) · 2.53 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
from twitchio.ext import commands
import random
import asyncio
import keyboard
import simpleaudio
from twitchapi import TwitchAPI
from sharedstate import shared_state
twitch = TwitchAPI()
class Bot(commands.Bot):
def __init__(self):
super().__init__(token=os.environ['TMI_TOKEN'], prefix='!', initial_channels=['catcam69000'])
async def event_ready(self):
print(f'chatbot ready | {self.nick}')
while not keyboard.is_pressed('ctrl+alt+z'):
await asyncio.sleep(0.1)
exit()
async def event_message(self, message):
if message.echo:
return
print(message.content)
await self.handle_commands(message)
async def send_msg(self, ctx, msg):
await ctx.send(f'[bot] {msg}')
@commands.command(name='meow')
async def meow(self, ctx):
user_id = str(ctx.author.id)
channel_id = str(ctx.channel.name)
if twitch.check_user_follows(user_id, channel_id):
await self.play_meow(ctx)
else:
await ctx.send('[bot] must be a follower to use !meow')
async def play_meow(self, ctx):
meow_folder = 'meows'
meow_files = [f for f in os.listdir(meow_folder) if f.endswith('.wav')]
if meow_files:
random_meow = random.choice(meow_files)
try:
# Define a function to play sound
def play_sound():
wave_obj = simpleaudio.WaveObject.from_wave_file(f'{meow_folder}/{random_meow}')
play_obj = wave_obj.play()
play_obj.wait_done()
# Run the play_sound function in a separate thread
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, play_sound)
await ctx.send('[bot] playing meow sound...')
except Exception as e:
await ctx.send(f'[bot] Error: {e}')
@commands.command(name='boxes')
async def toggle_boxes(self, ctx):
await ctx.send('[bot] hiding boxes...') if shared_state.get_show_boxes() else await ctx.send('[bot] showing boxes...')
shared_state.toggle_show_boxes()
@commands.command(name='text')
async def toggle_text(self, ctx):
await ctx.send('[bot] hiding text...') if shared_state.get_show_text() else await ctx.send('[bot] showing text...')
shared_state.toggle_show_text()
def start_bot():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
bot = Bot()
bot.run()