【问题标题】:Discord Bot ctx only capturing first wordDiscord Bot ctx 仅捕获第一个单词
【发布时间】:2020-08-19 21:30:18
【问题描述】:
from discord.ext import commands
import random

description = '''An example bot to showcase the discord.ext.commands extension
module.

There are a number of utility commands being showcased here.'''
bot = commands.Bot(command_prefix='?', description=description)

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




#here i need the help

@bot.command()
async def idea(ctx, content):
    """Repeats a message multiple times."""
    await ctx.send(content)
    f= open("supersmartidea.txt","a")
    f.write("¦" + content + "\n")

机器人只保护作为 ctx 输入的第一个单词,所以如果我输入 ?想法这是个好主意, 只有“这个”被写下来。 机器人应该写下“这是个好主意” 我以前从未编写过机器人代码,也不知道如何修复它。

【问题讨论】:

    标签: python bots discord ctx


    【解决方案1】:

    您可以通过两种方式获取全部内容。

    这将给出他们所说的字符串。例如?idea A good idea 将返回:A good idea

    @bot.command()
    async def idea(ctx, *, content):
        #code
    

    这将返回每个单词的元组。例如:?idea A good idea 将返回:('A', 'good', 'idea') 然后您可以通过 content = ' '.join(content) 将其转换为字符串

    @bot.command()
    async def idea(ctx, *content):
        #code
    

    【讨论】:

      【解决方案2】:

      这可以通过使用以下 .join 功能来实现。

      @bot.command()
      async def idea(ctx, content):
          """Repeats a message multiple times."""
          message = (" ").join(content)
          await ctx.send(message)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-16
        • 1970-01-01
        • 2020-08-17
        • 2015-02-26
        • 1970-01-01
        • 1970-01-01
        • 2018-06-04
        • 1970-01-01
        相关资源
        最近更新 更多