2
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from PIL import Image

Client = discord.Client()
client = commands.Bot(command_prefix = "-")`
@client.command
async def spam(ctx, arg):
    count = 0 
    await Context.send(message.channel, "Wait for it....")
    time.sleep(3)
    while count < 20:
        await ctx.send(arg)
        time.sleep(3)
        count = count + 1

This code is supposed to mention the person specified in the argument. For example if someone typed -spam @Bob the bot should say

@Bob
@Bob 20 times
4
  • So where is the Problem? What is the actual outcome of your program? I guess the command triggers right but your bot's response is not. You are just sending the play argument back. If you want to mention the User you to use mention right. One example can be found here stackoverflow.com/questions/51905563/… Commented Nov 22, 2018 at 23:46
  • My problem is that it doesn't actually print anything, including "Wait for it..." Commented Nov 23, 2018 at 3:43
  • Are you sure your bot is running? Your code is missing something like client.run('token') which would start the bot. Also, you haven't defined Context yet you use it. Perhaps this should be ctx? Which version of discord.py are you using? Commented Nov 23, 2018 at 5:21
  • Oh yea it is running, I forgot to add that to this, but in my actual code it is there. Everything else works, its just this command thing which is a bit sketchy. Commented Nov 23, 2018 at 17:32

2 Answers 2

1

There's no need to instantiate both discord.Client and commands.Bot. Bot is a subclass of Client.

Bot.command is a function that returns a decorator, not itself a decorator. You need to call it to use it to decorate a coroutine:

@client.command()

You should probably be using a converter to acquire the user you're pinging.

Context is the class that ctx is an instance of. You should be accessing all of the methods through ctx.

Don't use time.sleep, as it blocks the event loop. Await asyncio.sleep instead.

from discord.ext.commands import Bot
from asyncio import sleep
from discord import User

client = Bot(command_prefix = "-")

@client.command()
async def spam(ctx, user: User):
    await ctx.send("Wait for it....")
    await sleep(3)
    for _ in range(20):
        await ctx.send(user.mention)
        await sleep(3)

client.run("token")
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for helping me with this. time.sleep was indeed causing me issues. The above code gets an error with user: User async def spam(ctx, user: User): NameError: name 'User' is not defined What I was trying to get my code to do was ping the person specified in the argument 20 times. Sorry if I was unclear.
Did you include the from discord import User line?
Oops! I had forgotten to include that.
I tested this with adding the from line that I had forgotten. I am not receiving an error, but the bot doesn't actually send the message. Sorry if I made a simple mistake, I'm new to python. here is my code: pastebin.com/gr00DfVK
You need to specify which sleep you're using, and you also need to actually run your bot client.run(...)
0

To start, you don't need both discord.Client() and commands.Bot()

Here is a breakdown:

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from PIL import Image

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

@client.command()
async def spam(ctx, user:discord.Member): #Replace arg with user:discord.Member, discord.Member has many attributes that allow you to do multiple actions
    count = 0 
    await ctx.send(message.channel, "Wait for it....") #replace Context with ctx, ctx is the Context
    time.sleep(3)
    while count <= 20: # Replaced count<20 with count<=20 otherwise it'll only spam 19 times 
        await ctx.send(user.mention) #changed args to user.mention, user.mention mentions the user
        time.sleep(3)
        count = count + 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.