簡體   English   中英

如何在 python discord 機器人中獲取用戶輸入?

[英]How can I get user input in a python discord bot?

我有一個 python discord 機器人,我需要它在命令后獲取用戶輸入,我該怎么做? 我是 python 和制作 discord 機器人的新手。 這是我的代碼:

import discord, datetime, time
from discord.ext import commands
from datetime import date, datetime

prefix = "!!"
client = commands.Bot(command_prefix=prefix, case_insensitive=True)

times_used = 0

@client.event
async def on_ready():
  print(f"I am ready to go - {client.user.name}")
  await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{client.command_prefix}python_help. This bot is made by drakeerv."))

@client.command(name="ping")
async def _ping(ctx):
  global times_used
  await ctx.send(f"Ping: {client.latency}")
  times_used = times_used + 1

@client.command(name="time")
async def _time(ctx):
  global times_used
  from datetime import date, datetime

  now = datetime.now()

  if (now.strftime("%H") <= "12"):
    am_pm = "AM"
  else:
    am_pm = "PM"

  datetime = now.strftime("%m/%d/%Y, %I:%M")

  await ctx.send("Current Time:" + ' '  + datetime + ' ' + am_pm)
  times_used = times_used + 1

@client.command(name="times_used")
async def _used(ctx):
  global times_used
  await ctx.send(f"Times used since last reboot:" + ' ' + str(times_used))
  times_used = times_used + 1

@client.command(name="command") #THIS LINE
async def _command(ctx):
  global times_used
  await ctx.send(f"y or n")
  times_used = times_used + 1

@client.command(name="python_help")
async def _python_help(ctx):
  global times_used
  msg = '\r\n'.join(["!!help: returns all of the commands and what they do.",
                     "!!time: returns the current time.",
                     "!!ping: returns the ping to the server."])
  await ctx.send(msg)
  times_used = times_used + 1



client.run("token")

我正在使用 python 版本 3.8.3。 我已經看過其他帖子,但他們沒有回答我的問題或給我錯誤。 任何幫助將不勝感激!

你會想要使用Client.wait_for()

@client.command(name="command")
async def _command(ctx):
    global times_used
    await ctx.send(f"y or n")

    # This will make sure that the response will only be registered if the following
    # conditions are met:
    def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel and \
        msg.content.lower() in ["y", "n"]

    msg = await client.wait_for("message", check=check)
    if msg.content.lower() == "y":
        await ctx.send("You said yes!")
    else:
        await ctx.send("You said no!")

    times_used = times_used + 1

並有超時:

import asyncio # To get the exception

@client.command(...)
async def _command(ctx):
    # code
    try:
        msg = await client.wait_for("message", check=check, timeout=30) # 30 seconds to reply
    except asyncio.TimeoutError:
        await ctx.send("Sorry, you didn't reply in time!")

參考:

有了這個,你可以做這樣的事情

@client.command()
async def command(ctx):
    computer = random.randint(1, 10)
    await ctx.send('Guess my number')

    def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel and int(msg.content) in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    msg = await client.wait_for("message", check=check)

    if int(msg.content) == computer:
        await ctx.send("Correct")
    else:
        await ctx.send(f"Nope it was {computer}")

我花了幾天時間試圖弄清楚這一點,對我來說唯一簡單的事情就是將 message.content 存儲到一個列表中並稍后使用它。

some_list = []
    user_message = message.content.split()
    some_list.append(user_message)

因為我使用的是命令,所以我想刪除那個'!' 並獲取原始消息

        for i  in some_list:
        del i[0]# delete's command tag

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM