I need some help figuring out how to modify existing code to add a new feature.
There is an open source chat bot named Nortbot. It's used for a chat website named Tinychat; for which I use with friends. Nortbot is written in Python 2.7. The bot can do many things but the feature I'm focused on is it's ability to send messages to the chat, It can greet people that join, or it can respond to a commands like !flip and it will say "The coin is tails", etc.
What I'm attempting to do is modify this bot so that it will relay messages from a Discord channel. My plan was to have Nortbot say messages from a text file which acts as a chat log from discord. I've made a discord bot that logs the messages to a text file already. I'm just really struggling to find a way to get Nortbot to automatically send the messages to the TinyChat room.
Some snips of code from Nortbot that seem possibly relevant to this goal is a command function:
I chopped up the file to keep things short but these seems to be some of the key functions to responding to a command:
Code
class CommandHandler:
def __init__(self, bot, user, msg, config, pool):
self._bot = bot
self._user = user
self._msg = msg
self._conf = config
self._pool = pool
def _handle_command(self, cmd, cmd_arg):
log.debug('handling command `%s`, args: %s, user: %s' %
(cmd, cmd_arg, self._user))
elif cmd == 'flip':
self.do_flip_coin()
def do_flip_coin(self):
self._responder('The coin was: %s' % locals_.flip_coin())
For another example, such as a greeting message it uses some code like this:
Code
class JoinHandler(Check):
def __init__(self, bot, user, config):
super(JoinHandler, self).__init__(bot, user, config)
self._bot = bot
self._user = user
self._conf = config
def _greet(self):
self._bot.responder('Welcome to the room %s:%s' %
(self._user.nick, self._user.handle),
timeout=self._bot.rand_float())
My idea so far has been to alter / duplicate these functions to implement my own messages being sent from a text file.
So far what I've been able to do is get Nortbot to print the discord messages in the console. I wrote a function to do this. But I want it to send the messages to the chat room, not just print them in the console. How my function works is checks if the file is empty; if it has something in it, print it to the console, then delete the text from the chat log file so it's ready for the next message. I used threading to run this code in a loop so it doesnt block the rest of the program from doing things. For what it's worth here is the function I wrote:
Code
def chatrelay():
while True:
f = open(r'G:\fromdiscord.txt', "r+")
if os.stat(r"G:\fromdiscord.txt").st_size == 0:
f.close()
time.sleep(3)
else:
speak = f.read()
print speak
f.truncate(0)
f.close()
time.sleep(3)
if __name__ == "__main__":
t = threading.Thread(target=chatrelay)
t.setDaemon(True)
t.start()
while True:
time.sleep(3)
pass
What I really want to do is find a way to send variable
speak to the chatroom, in a similar way that it does in the first to code examples. Except I need it to be done automatically (some delay is fine).
I am a newbie at programming so I'm struggling to understand the existing code on Nortbot. I've spent days reading guides, documentation, and experimenting... and largely failing. I google every error i get but the 'solutions' i find on sites like stackoverflow have rarely solved my errors. I've been using PyCharm to edit the code and using interpreter Python 2.7.
Sorry for the long post. I'm posting out of desperation. I'm not asking somebody to code for me, but maybe just some advice on how I can deconstruct the way Nortbot works. The bot can be found on github but It seems posting links here is against the rules. Northbot has MIT License, which means it's legal to use, copy, modify, merge, publish, distribute, etc.
All help is appreciated.
This post was edited by NatureNames on Nov 22 2019 07:39am