L
L
Lol Likovic2020-07-19 15:41:06
Python
Lol Likovic, 2020-07-19 15:41:06

The bot code for sending a message works, but the bot does not write to the chat, how can I fix this?

And so, I write the same bot:

import discord
from discord.ext import commands
from discord.ext import tasks
from time import time, localtime

user_counter = {}

msg = 'Статистика:\n'

client = commands.Bot( command_prefix = '-')

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))




@client.event
async def on_message(message):
    if message.author == client.user:
        return
    else:
    	try:
    		user_counter[message.author.id] += 1
    	except KeyError:
    		user_counter[message.author.id] = 1
    await client.process_commands(message)

@client.command( pass_context = True)
async def globalstat(ctx):
    for user_id,counter in user_counter.items():
        global msg
        user = client.get_user(user_id)
        msg = msg+' >> ' + user.name + ': ' + str(counter)  + '\n'
        await ctx.send(msg)


@tasks.loop(seconds=20)
async def timer_notify():
    await client.wait_until_ready()
    cur_time = localtime(time())
    for user_id,counter in user_counter.items():
        global msg
        user = client.get_user(user_id)
        msg = msg+' >> ' + user.name + ': ' + str(counter)  + '\n'
    msg = 'Статистика:\n'

We are interested in this part of the code
@tasks.loop(seconds=20)
async def timer_notify():
    await client.wait_until_ready()
    cur_time = localtime(time())
    for user_id,counter in user_counter.items():
        global msg
        user = client.get_user(user_id)
        msg = msg+' >> ' + user.name + ': ' + str(counter)  + '\n'
    msg = 'Статистика:\n'

And so, no matter how I write, or await message.channel.send(msg)
even await channel.send(msg)
write without await, nothing happens. But the code itself is working, it works through print.
So, how to ask the bot to be supportive of me and write a message to the channel?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
marselabdullin, 2020-07-19
@Lolik666

I don’t know what exactly doesn’t work for you, but the fact that you are constantly overwriting msg in these lines is not normal:

msg = msg+' >> ' + user.name + ': ' + str(counter)  + '\n'
    msg = 'Статистика:\n'

Maybe you should use +=

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question