Discord.py bot wont print a SIMPLE variable in the

2020-01-19 08:08发布

What I want is for the bot to say Snacktoshis: 5, but it won't print the variable in the chat.

Here is my code:

from discord import *
from discord.ext import *
from discord.ext.commands import Bot
from discord.ext.commands import *
import random
import asyncio
from discord import Game
from discord.ext.commands import Bot
import aiohttp
import discord
import requests
import time

BOT_PREFIX = ("?", "!")
client = Bot(command_prefix=BOT_PREFIX)

TOKEN ="3812236138921603126360103210983inserttoken21831382ufsfuadha"



@client.command()
async def bal():
    snack = 0
    await client.say("Snacktoshis:",snack)


client.run(TOKEN)

My error message is:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "/usr/local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: send_message() takes from 2 to 3 positional arguments but 4 were given

I know I have alot of unused modules.

2条回答
啃猪蹄的小仙女
2楼-- · 2020-01-19 08:16

From help('discord.ext.commands.Bot.say'):

discord.ext.commands.Bot.say = say(self, *args, **kwargs)

A helper function that is equivalent to doing

    self.send_message(message.channel, *args, **kwargs)

So the four arguments that are being sent into send_message from your client.say call are client, message.channel, "Snacktoshis:", and snack. You want the message you're sending to be a single string, instead of two separate arguments.

await client.say("Snacktoshis: " + snack)

or

await client.say("Snacktoshis: {}".format(snack))
查看更多
等我变得足够好
3楼-- · 2020-01-19 08:31

I assume what you mean is something like this:

@bot.command()
async def bal():
  snack = 0
  await bot.say("Snacktoshis: %d" %snack)

Where the variable should be part of the message, and not a separate argument to the method call.

查看更多
登录 后发表回答