Devblog #2: a simple task for a bot

in #blog6 years ago (edited)

Postheader Logo

Welcome to the second part of my development blog.

This blog post will be about a little gaming project I came up with. You can read in more detail what it's all about in the introduction post.

deutschbot

As I mentioned in the last part I was using deutschbot to learn a bit about how things are done. This bot has a couple of nice features I want to use later on so I started using it as a base and forked the github repository.

To have some kind of discord integration already set up is nice thing to have. In addition there is some database functionality with user management.

my python code

Last time I showed the routine I had to rewrite into Python. I had more problems than expected coming along with python. Some things feel a little bit alien to me because I'm used to write php and JavaScript. Starting with an existing project is nice because you already have so many things you can use and learn from. On the other hand you almost certainly know you will make a mess out of the code because you don't know what you are doing and you will use things wrong.

Starting from scratch is a pain and will take much longer until you get something actually working. You would learn from the start and maybe understand better what you are doing. But with a project in mind I certainly don't have the patience to do that. So I just added my stuff to deutschbot.

To run it I added a new argument 'playgame' for --run calling start_game() from the new communitybot/playgame.py

def start_game(self):

    #settings
    duration_hours = 0
    duration_minutes = 0
    duration_seconds = 5

    limit = 2

    # 1. create post
    title = 'post title'
    body = 'post body'
    permlink = self.post(title, body, self.bot_account)

    # 2. catch upvotes and create comments
    postid = "@" + self.bot_account + "/" + permlink;
    start = time.time()
    duration = duration_hours * 360 + duration_minutes * 60 + duration_seconds

    voters = []
    permlinks ={}

    while time.time() < start + duration:
        if len(voters) < limit :
            votes = self.s.get_active_votes(self.bot_account, permlink)

            if len(votes) > 0 :
                v_votes = self.get_valid_votes(votes, limit)

                for vote in v_votes:
                    if vote['voter'] not in voters:
                        comment_body = 'comment created for ' + vote['voter']
                        voters.append(vote['voter'])
                        permlinks[vote['voter']] = self.post('', comment_body, self.bot_account, postid)

        time.sleep(5)

    # 3. post summary
    results = {}
    for voter in voters:
        votes = self.s.get_active_votes(self.bot_account, permlinks[voter])

        results[voter] = len(votes)

    result_budy = self.evaluate(results)
    self.post('', result_budy, self.bot_account, postid)

Without doing real post operations this seems to run fine. Here is the output with some test data:

output

In this test I took my introduction post and fetched its votes. Then I chose two comments from @kurodevs and @flurgx on this post and counted the votes on them.

Discord integration

Currently I'm working on some Discord integration. Basically I want to start a game by telling a bot on Discord to do so and get some information back about what's going on, e.g. who participates and who won at the end of a game.

Right now I'm stuck there, so if you're well versed with python and want to give me a hand I welcome you to let me know.

When this is done I will start a first try with actual posting content on steem. Here a quick remembrance on what I want it to do:

  1. create a post on command
  2. catch upvotes on this post and create a comment with upvoters name
  3. create another comment after a given time displaying upvote informations of comments created in the last step

I'll check back on you when everything is running and I need your Help with some upvotes to try it out. Since I just learned you can disable rewards I will set this for the test. If you want to support me with this project I would highly appreciate an upvote on the introduction post.

See you on steem.

Sort:  

Sehr gut gestalteter Artikel! Verfolge diesen Blog wirklich sehr gerne und hoffe für dich, dass das so klappt, wie du es dir vorstellst :)

Coin Marketplace

STEEM 0.19
TRX 0.14
JST 0.029
BTC 65070.55
ETH 3147.10
USDT 1.00
SBD 2.55