Browse Source

splitted the bot in multiple files for commands

master
Georges-Emile Bihr 5 months ago
parent
commit
f0b4ffa7bb
  1. 5
      src/commands/__init__.py
  2. 3
      src/commands/hello.py
  3. 11
      src/commands/help.py
  4. 4
      src/commands/ping.py
  5. 0
      src/commands/quit.py
  6. 25
      src/commands/read.py
  7. 20
      src/commands/tell.py
  8. 50
      src/main.py

5
src/commands/__init__.py

@ -0,0 +1,5 @@
from .hello import hello
from .help import send_help
from .ping import ping
from .read import read, read_messages
from .tell import tell

3
src/commands/hello.py

@ -0,0 +1,3 @@
def hello(bot, channel, user):
bot.send(channel, f"Hello, {user}!")
return

11
src/commands/help.py

@ -0,0 +1,11 @@
def send_help(bot, channel: str, user: str) -> None:
bot.send(channel, f"Ohai? Someone called me? Oh, it's {user}!")
bot.send(channel, f"Sorry, I didn't really hear you, you should add \"{bot.prefix}\" before your message, so I could hear better, nya!")
bot.send(channel, "Or maybe there was? unyaa~!")
bot.send(channel, "Anyway, the available commands are :")
bot.send(channel, " hi/hello, ping, tell, and read!")
if user in bot.admins:
bot.send(channel, "And for you, master, I can allow you to use quit! Mrrr~")
bot.send(channel, "I'm thirsty after all this talking! Master, master! Can I have a bowl of water, pleaaaaase? =^w^=")
return

4
src/commands/ping.py

@ -0,0 +1,4 @@
from time import time as timestamp
def ping(bot, channel):
bost.send(channel, f"Pong! ({timestamp()})")
return

0
src/commands/quit.py

25
src/commands/read.py

@ -0,0 +1,25 @@
def read(bot, user):
if not read_messages(bot, user):
bot.send(user, "You don't have any message in you messageboard!")
return
def read_messages(bot, user) -> bool:
messages_found = False
if "msgboard" in bot.sdict.keys():
messages = []
for i in range(len(bot.sdict["msgboard"])):
if bot.sdict["msgboard"][i]["dest"] == user:
messages.append(bot.sdict["msgboard"][i])
if len(messages) > 0:
messages_found = True
bot.send(user, f"You have {len(messages)} new messages:")
for message in messages:
if message["dest"] == user:
from_user = message["from"]
msg_content = message["msg"]
bot.send(user, f"from {from_user}: \"{msg_content}\"")
bot.sdict["msgboard"].remove(message)
bot.save_storage()
bot.reload_storage()
return messages_found

20
src/commands/tell.py

@ -0,0 +1,20 @@
def tell(bot, msg, user, channel):
if len(bot.storage) > 0:
if len(msg.split()) > 1:
msgdict = {}
msgdict["from"] = user
msgdict["dest"] = msg.split()[0]
msgdict["msg"] = msg[len(msgdict["dest"])+1:]
if "msgboard" not in bot.sdict.keys():
bot.sdict["msgboard"] = []
bot.sdict["msgboard"].append(msgdict)
bot.save_storage()
print(f"message to {msgdict['dest']} added: {msgdict['msg']}")
bot.send(channel, "Message sent!")
else:
bot.send(channel, "Awww, sorry, I couldn't find the one you wanted to contact, mew :/")
else:
bot.send(channel, "Oh, but, uh, there's not message board ._.")
bot.send(channel, "This function is unavailable at the moment, sornya!!!")
return

50
src/main.py

@ -3,6 +3,7 @@ import os
import random
from geem_creds import server_dict
import pickle
import commands
# # IRC config
# create a file named geem_creds.py with this dict:
@ -49,43 +50,23 @@ class GeemBot(IRCBot):
) -> None:
print(f"Command received: {msg}")
if (len(msg.strip()) <= 0):
self.send_help(channel, user)
return
return commands.send_help(self, channel, user)
command = msg.split()[0].lower()
msg = msg[len(command)+1:]
if command in ["hi", "hello"]:
self.send(channel, f"Hello, {user}!")
return
return commands.hello(self, channel, user)
elif ((command == "quit") and (user in self.admins)):
self.send(channel, "Bye, master!")
self.should_stop()
return
elif command == "ping":
self.send(channel, "Pong!")
return
return commands.ping(self, channel)
elif command == "tell":
if len(self.storage) > 0:
if len(msg.split()) > 1:
msgdict = {}
msgdict["from"] = user
msgdict["dest"] = msg.split()[0]
msgdict["msg"] = msg[len(msgdict["dest"])+1:]
if "msgboard" not in self.sdict.keys():
self.sdict["msgboard"] = []
self.sdict["msgboard"].append(msgdict)
self.save_storage()
print(f"message to {msgdict['dest']} added: {msgdict['msg']}")
self.send(channel, "Message sent!")
else:
self.send(channel, "Awww, sorry, I couldn't find the one you wanted to contact, mew :/")
else:
self.send(channel, "Oh, but, uh, there's not message board ._.")
self.send(channel, "This function is unavailable at the moment, sornya!!!")
return commands.tell(self, msg, user, channel)
elif command == "read":
if not self.read_messages(user):
self.send(user, "You don't have any message in you messageboard!")
return commands.read(self, user)
elif command == "help":
self.send_help(channel, user)
return commands.send_help(self, channel, user)
def read_messages(self, user) -> bool:
messages_found = False
@ -107,17 +88,6 @@ class GeemBot(IRCBot):
self.reload_storage()
return messages_found
def send_help(self, channel: str, user: str) -> None:
self.send(channel, f"Ohai? Someone called me? Oh, it's {user}!")
self.send(channel, f"Sorry, I didn't really hear you, you should add \"{self.prefix}\" before your message, so I could hear better, nya!")
self.send(channel, "Or maybe there was? unyaa~!")
self.send(channel, "Anyway, the available commands are :")
self.send(channel, " hi/hello, ping, tell, and read!")
if user in self.admins:
self.send(channel, "And for you, master, I can allow you to use quit! Mrrr~")
self.send(channel, "I'm thirsty after all this talking! Master, master! Can I have a bowl of water, pleaaaaase? =^w^=")
return
def handle_message(self, msg: str) -> None:
user_space = msg.find(' ')
user = msg[:user_space]
@ -153,15 +123,15 @@ class GeemBot(IRCBot):
return self.handle_command(channel, user, msg, is_channel)
elif "sanil" in msg.lower():
if is_channel:
return self.send_help(channel, user)
return commands.send_help(self, channel, user)
else:
return self.send_help(user, user)
return commands.send_help(self, user, user)
elif irc_command == "JOIN":
channel = msg.split()[0].replace(':', '').strip()
print(f"{user} joined {channel}")
if user in self.admins:
self.send(channel, f"Hello, master {user}!")
self.read_messages(user)
commands.read_messages(self, user)
return
elif irc_command == "PART":
channel = msg.split()[0].replace(':', '').strip()

Loading…
Cancel
Save