Answer the question
In order to leave comments, you need to log in
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'
@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'
await message.channel.send(msg)
await channel.send(msg)
Answer the question
In order to leave comments, you need to log in
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'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question