Updated plugins to work with asyncio.

This commit is contained in:
Yuuki Chan 2024-02-22 18:42:31 +09:00
parent b1f6ca23c5
commit bc8424c1c8
2 changed files with 25 additions and 30 deletions

View file

@ -5,55 +5,50 @@ from src.plugin_base import PluginBase
class Plugin(PluginBase):
def handle_message(self, source_nick, channel, message):
async def handle_message(self, source_nick, channel, message):
message_parts = message.split()
self.channel_manager = ChannelManager()
if message_parts[0] == '!hello':
self.bot.ircsend(f'PRIVMSG {channel} :Hello, {source_nick}!')
if message_parts[0] == '!hello':
await self.bot.ircsend(f'PRIVMSG {channel} :Hello, {source_nick}!')
elif message_parts[0] == '!join':
if len(message_parts) == 0:
self.bot.ircsend(f'PRIVMSG {channel} :Please specify a channel to join')
await self.bot.ircsend(f'PRIVMSG {channel} :Please specify a channel to join')
return
else:
self.channel_manager.save_channel(message_parts[1])
self.bot.ircsend(f'JOIN {message_parts[1]}')
await self.bot.ircsend(f'JOIN {message_parts[1]}')
elif message_parts[0] == '!part':
if len(message_parts) == 1:
self.bot.ircsend(f'PART {channel}')
await self.bot.ircsend(f'PART {channel}')
self.channel_manager.remove_channel(channel)
else:
self.bot.ircsend(f'PART {message_parts[1]}')
await self.bot.ircsend(f'PART {message_parts[1]}')
self.channel_manager.remove_channel(message_parts[1])
elif message_parts[0] == '!quit':
if len(message_parts) == 0:
quit_message = 'EliteBot!'
else:
quit_message = message[len(message_parts[0]) + 1:]
self.bot.ircsend(f'QUIT :{quit_message}')
self.bot.ircsock.close()
await self.bot.ircsend(f'QUIT :{quit_message}')
await self.bot.ircsock.close()
self.bot.connected = False
sys.exit()
elif message_parts[0] == '!raw':
if len(message_parts) > 1:
if message_parts[1].upper() == 'PRIVMSG' and len(message_parts) > 3:
raw_command = ' '.join(message_parts[1:3]) + " :" + ' '.join(message_parts[3:])
else:
raw_command = ' '.join(message_parts[1:])
self.bot.ircsend(raw_command)
await self.bot.ircsend(raw_command)
elif message_parts[0] == '!me':
if len(message_parts) > 1:
action_message = ' '.join(message_parts[1:])
self.bot.ircsend(f'PRIVMSG {channel} :\x01ACTION {action_message}\x01')
await self.bot.ircsend(f'PRIVMSG {channel} :\x01ACTION {action_message}\x01')
else:
self.bot.ircsend(f'PRIVMSG {channel} :Please specify an action')
await self.bot.ircsend(f'PRIVMSG {channel} :Please specify an action')
elif message_parts[0] == '!ping':
if len(message_parts) > 1:
self.bot.ircsend(f'PRIVMSG {channel} :Pinging {message_parts[1]}')
await self.bot.ircsend(f'PRIVMSG {channel} :Pinging {message_parts[1]}')
else:
self.bot.ircsend(f'PRIVMSG {channel} :Please specify a nick to ping')
await self.bot.ircsend(f'PRIVMSG {channel} :Please specify a nick to ping')

View file

@ -22,7 +22,7 @@ df = '%Y/%m/%d %H:%M:%S'
class Plugin(PluginBase):
def handle_message(self, source_nick, channel, message):
async def handle_message(self, source_nick, channel, message):
parts = message.split()
c_db.create_table('Cookie')
self.channel_manager = ChannelManager()
@ -31,9 +31,9 @@ class Plugin(PluginBase):
if len(parts) == 1:
self.insert_user(source_nick)
cookies = c_db.get(source_nick, 2)
cookies = c_db.get_user(source_nick, 2)
rnd = random.randint(1, 10)
last = datetime.strptime(c_db.get(source_nick, 3), df)
last = datetime.strptime(c_db.get_user(source_nick, 3), df)
current = datetime.strptime(datetime.now().strftime(df), df)
diff = round((current - last).total_seconds() / 60.0)
@ -45,27 +45,27 @@ class Plugin(PluginBase):
else f'{(cookies + rnd)} cookie' if (cookies + rnd) == 1 \
else f'{(cookies + rnd)} cookies'
self.bot.ircsend(f'PRIVMSG {channel} :\x01ACTION gives {c1} to {source_nick}.\x01')
self.bot.ircsend(f'PRIVMSG {channel} :You now have a total of {c2}.')
await self.bot.ircsend(f'PRIVMSG {channel} :\x01ACTION gives {c1} to {source_nick}.\x01')
await self.bot.ircsend(f'PRIVMSG {channel} :You now have a total of {c2}.')
c_db.set(source_nick, {
c_db.set_user(source_nick, {
'cookies': (cookies + rnd),
'last': current.strftime(df)
})
else:
rem = self.remaining_time(last.strftime(df), 30 * 60000)
self.bot.ircsend(f'PRIVMSG {channel} :Remaining time: {rem}')
await self.bot.ircsend(f'PRIVMSG {channel} :Remaining time: {rem}')
elif len(parts) == 2:
cookies = c_db.get(parts[1], 2)
cookies = c_db.get_user(parts[1], 2)
if cookies == -1:
self.bot.ircsend(f'PRIVMSG {channel} :I\'ve looked everywhere for {parts[1]}, but I couldn\'t '
f'find them.')
await self.bot.ircsend(f'PRIVMSG {channel} :I\'ve looked everywhere for {parts[1]}, but I couldn\'t '
f'find them.')
else:
c = 'no cookies' if cookies == 0 \
else f'{cookies} cookie' if cookies == 1 \
else f'{cookies} cookies'
self.bot.ircsend(f'PRIVMSG {channel} :{parts[1]} currently has {c}.')
await self.bot.ircsend(f'PRIVMSG {channel} :{parts[1]} currently has {c}.')
def insert_user(self, user: str):
with c_db.engine.connect() as conn: