0

Script expected to send all commands to Twitch chat in random order and within specified time period then loop indefinitely, but despite showing success in command window, it actually sends only the first message in Twitch chat. This code is not my, and I’m in no way a programmer or scripter, nevertheless I’ve killed a whole day to study all this mumbo-jumbo, so I can understand your nerdish answer pretty decently. Don’t punch in the face, though. =^_^=

#!/usr/bin/env python

from time import sleep
from random import randint, shuffle
from sys import argv
from twitchio.ext import commands

import logging
logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO)

class Bot(commands.Bot):

    def __init__(self):
        super().__init__(
                        token=argv[1],
                        prefix='',
                        initial_channels=['#channel_name']
                )

    async def event_ready(self):
        lst = ['command_1', 'command_2', 'command_3']

        while True:
            shuffle(lst)
            for x in lst:
                logging.info(f'{self.nick} is sending: {x}')
                await bot.connected_channels[0].send(x)
                sleep(randint(210,270))
            logging.info('Sent all msgs, sleeping...')
            sleep(randint(270,300))
            logging.info(f'Slept!\n')

bot = Bot()
bot.run()

Sending all three messages Recieving only one message

I'd like to receive all three of my sexy commands in Twitch chat after sending them. And indefinite loop afterwords would be pretty awesome too. Thanks for your time and sorry for my lame English (not a native speaker). Cheers!

  • The code seems ok and there were no errors logged. Do you know the interval that you need to wait before sending a new message to that channel? – Quan VO Mar 28 '23 at 23:13
  • I can message instantly in those channel’s Twitch chat whatever amount of messages I want. So if this code is OK, it’s something on my side, I guess. Win 7, maybe? Goddammit… BTW, I start it through the .bat: `start python ChtBt.py ARGUMENT` Generating ARGUMENT with correct user login [here](https://twitchtokengenerator.com/). – mydarkhalf Mar 29 '23 at 04:46
  • Maybe you can try with another channel to see if there is any difference. – Quan VO Mar 29 '23 at 22:08
  • Same problem on other channels. Only the first message appears. Maybe it's a Twitchio problem? Maybe I should install some dependencies which I didn't? Or maybe there is another code example to randomly send messages to chat? – mydarkhalf Mar 29 '23 at 22:39
  • In this line `await bot.connected_channels[0].send(x)`, using `bot` here may lead to unexpected behavior. The reason why the code works is because there is a global variable naming `bot`. Please replace `await bot.connected_channels[0].send(x)` by `await self.connected_channels[0].send(x)` then try again. – Quan VO Mar 29 '23 at 23:54
  • `Self` changes nothing, [unfortunately](https://drive.google.com/file/d/1rr9DnpSncKt55vdgNBuM0MVRDDUvBay1/view?usp=share_link). Maybe after sending first message this script somehow forgets my ARGUMENT and logs me out? – mydarkhalf Mar 30 '23 at 01:46

0 Answers0