d2jsp
d2jsp Forums > Programmer's Haven > Modifying Python 2.7 Code.
Add Reply New Topic New Poll
NatureNames
#1 Nov 22 2019 07:30am
Group: Member
Posts: 14,891
Joined: Nov 29 2008
Gold: 0.00
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
EViLDeeDz
#2 Nov 22 2019 12:10pm
Group: Member
Posts: 9,263
Joined: Aug 13 2007
Gold: 1,500.00
Have you looked into the discord api?

They probably have a way for you to connect the bot directly to discord rather than reading the txt file.

This post was edited by EViLDeeDz on Nov 22 2019 12:11pm
Klexmoo
#3 Nov 22 2019 06:26pm
Group: Member
Posts: 8,235
Joined: May 17 2013
Gold: 52,291.00
Seems you can send a chat message using the client:
https://github.com/nortxort/nortbot/blob/67ed337d0adc92e5a3e01f9ec5657a39cf04c1b1/tinychat.py#L816

basically replace the print with a call to that method would work.

If you need help with instantiating the client and running this code, add my Discord Klex#3053 and I'll give you some assistance

This post was edited by Klexmoo on Nov 22 2019 06:26pm
NatureNames
#4 Nov 22 2019 11:56pm
Group: Member
Posts: 14,891
Joined: Nov 29 2008
Gold: 0.00
Quote (EViLDeeDz @ Nov 22 2019 10:10am)
Have you looked into the discord api?

They probably have a way for you to connect the bot directly to discord rather than reading the txt file.


That may be. But either way I think the problem remains. I can't figure out how to get the bot to send a string as a message to the chat room in an autonomous manner. I'm able to export and import the lines of text successfully between the two bots so I'm content with doing it that way unless it's causing a problem. Discord py is based on Python 3.8 and Northbot is using 2.7. The less work I have to do, the better. I know the text file thing is gimmicky but it's simple and I think it should work.

Quote (Klexmoo @ Nov 22 2019 04:26pm)
Seems you can send a chat message using the client:
https://github.com/nortxort/nortbot/blob/67ed337d0adc92e5a3e01f9ec5657a39cf04c1b1/tinychat.py#L816

basically replace the print with a call to that method would work.

If you need help with instantiating the client and running this code, add my Discord Klex#3053 and I'll give you some assistance


That has been my attempted strategy thus far. I've been trying to call the relevant class, function, and/or method into my chatrelay() function in place of print. But every attempted to do this has failed. I get errors such as:
'function' object has no attribute
unbound method must be called with instance as first argument
TypeError: unbound method send_chat_msg() must be called with Client instance as first argument (got nothing instead)
TypeError: function() takes at least 1 argument (0 given)

Lets say I wanted to make use of that send_chat_msg(self, msg): you linked to. To my understanding I would need to do something like

Client.send_chat_msg(msg=speak)
But when I run it, i get this:
TypeError: unbound method send_chat_msg() must be called with Client instance as first argument (got nothing instead)


I'll add you on discord. Thank you!
NatureNames
#5 Nov 23 2019 07:50am
Group: Member
Posts: 14,891
Joined: Nov 29 2008
Gold: 0.00
A BIG Thank You to Klexmoos https://forums.d2jsp.org/user.php?i=995794

For helping me out. And I learned a lot.


This post was edited by NatureNames on Nov 23 2019 08:14am
Go Back To Programmer's Haven Topic List
Add Reply New Topic New Poll