S
S
SANFY2020-11-28 12:29:25
Python
SANFY, 2020-11-28 12:29:25

What to do with this error " Ignoring exception in on_message Traceback (most recent call last): "?

I wrote a discord bot, this chat bot on command will send the cost of the bitcoin cryptocurrency (Bitcoin).
When run, it gives an error:
[command]: btcprice
Ignoring exception in on_message
Traceback (most recent call last):

Code itself:

import discord
import asyncio
import requests
import nest_asyncio
nest_asyncio.apply()

DISCORD_BOT_TOKEN = 'секрет"

BTC_PRICE_URL_coinmarketcap = 'https://api.coinmarketcap.com/v1/ticker/bitcoin/?c...'

client = discord.Client()

@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')

@client.event
async def on_message(message):
if message.content.startswith('!btcprice'):
print('[command]: btcprice ')
btc_price_usd, btc_price_rub = get_btc_price()
await client.send_message(message.channel, 'USD: ' + str(btc_price_usd) + ' | RUB: ' + str(btc_price_rub))

def get_btc_price():
r = requests.get(BTC_PRICE_URL_coinmarketcap)
response_json = r.json()
usd_price = response_json[0]['price_usd']
rub_rpice = response_json[0]['price_rub']
return usd_price, rub_rpice

client.run(DISCORD_BOT_TOKEN)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chaka YS, 2020-11-28
@Chaka_1

Not

await client.send_message(message.channel, 'USD: ' + str(btc_price_usd) + ' | RUB: ' + str(btc_price_rub))

and
await channel.send()
Before that, specify the channel ID for the channel where the message should be sent, that is:
channel = message.channel
await channel.send("Куприв")

Well, in general, why did you do it through on_message? You can also create a function and zero problems:
async def btcprice(ctx):
    channel = ctx.channel
    print('[command]: btcprice ')
    btc_price_usd, btc_price_rub = get_btc_price()
    await channel.send('USD: ' + str(btc_price_usd) + ' | RUB: ' + str(btc_price_rub))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question