简体   繁体   中英

I'm getting confused (new to programming)

So I wanna make a discord bot in python, that goes like that

import discord
client = discord.Client()
import time


@client.event
async def on_message(message):

    while message.content.startswith("---start"):
        time.sleep(2.5)
        await message.channel.send("a")
        if message.content.startswith("---stop"):
            break


client.run('token')

I want the bot to stop write "a" by typing in ---stop into discord, but I somehow don't know how to do that.

Why is there a while loop? The on_message function is run every time a message is posted - the value never changes. What you need is an if elif block:

import discord
import time
client = discord.Client()

spamming = False

@client.event
async def on_message(message):
    if message.content.startswith("---start"):
        spamming = True
        while spamming:
            time.sleep(2.5)
            await message.channel.send("a")
    elif message.content.startswith("---stop"):
        spamming = False

client.run('token')

I'm not 100% sure this works (I didn't check), but your code should be something similar.

import discord
import time
from asyncio import TimeoutError

client = discord.Client()

@client.event
async def on_message(message):
    if message.content.startswith("---start"): 
        global stop_spamming # Makes a global variable that allows stop_message_check function to view this variable
        stop_spamming = False

        def stop_message_check(message):
            if (message.content == '---stop'):
                global stop_spamming # Change the stop spamming variable to true once someone has written ---stop within 2.5 seconds of the last "a"
                stop_spamming = True

        while (stop_spamming == False): # Keeps running the code down below until ---stop has been said
            try:
                await message.channel.send("a") # Sends the letter "a" every 2.5 seconds assuming ---stop has not been said
                msg = await client.wait_for('message', check=stop_message_check, timeout = 2.5) # Waits 2.5 seconds for anyone to type "---stop"

            except TimeoutError:
                pass # If nobody said ---stop within 2.5 seconds of the last "a", a timeout error will be thrown, this was written here so the code doesn't exit out

client.run(token)

I used wait_for in this case to check when someone has used "---stop". I created a variable to tell the while loop when to stop spamming. The bot sends "a" every 2.5 seconds, that can be adjusted in the timeout variable of the client.wait_for line.

Here is the documentation on client.wait_for https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=wait#discord.ext.commands.Bot.wait_for

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM