【问题标题】:How do I use arguments in discord.py? [duplicate]如何在 discord.py 中使用参数? [复制]
【发布时间】:2020-03-19 15:05:41
【问题描述】:

好的,我有这段代码,但是当我尝试使用 $announce 时,它​​不会回写 Hello。事实上,它没有响应我放在 async def 下的任何内容。怎么会?

我觉得我所做的一切都是正确的,并且这些教程是用于重写和我正在使用的。

import discord
from discord.ext import commands
import os
import time
import random
import weather
from weather import Weather, Unit

prefix = '$'
client = commands.Bot(command_prefix = prefix)
clattr = discord.Client()


@client.event
async def on_ready():
        await client.change_presence(status = discord.Status.online, activity = discord.Game('have a nice day!'))
        print("Bot is now online!")


@client.event
async def on_message(message):
  if message.content.startswith("$hello"):
    await message.channel.send(f"""Hi""")
    await message.channel.send(f"""Hi""")
    await message.channel.send(f"""Hi""")
    await message.channel.send(f"""Hi""")
    await message.channel.send(f"""Hi""")
    await message.channel.send(f"""Hi""")
    await message.channel.send(time())
  if message.content.startswith("$dm"): 
    user = client.get_user(393921960817721355)
    dm_channel = user.dm_channel
    if (dm_channel==None):
      dm_channel = await user.create_dm()
    await dm_channel.send(f"""Hello!""")
  if message.content.startswith("$servers"):
    await message.channel.send(clattr.guilds)
  if message.content.startswith("$help"):
    await message.channel.send(f"""This is the vanceBot. Made by vance and OYOHO#5964 (xray#4398 helped as well).\nThese are the current commands:\n$hello, which says "Hi" a few times \n$dm, which DMs a person\n$servers, which displays the number of servers that the bot is on\n$leave, to get the bot to leave\n$help, this command.\nThe prefix is $.""")
  if message.content.startswith("$pinghelp"):
    await message.channel.send(f"""||@everyone|| This is the vanceBot. Made by vance and OYOHO#5964 (xray#4398 helped as well).\nThese are the current commands:\n$hello, which says "Hi" a few times \n$dm, which DMs a person\n$servers, which displays the number of servers that the bot is on\n$leave, to get the bot to leave\n$help, this command.\nThe prefix is $.""")


@client.command()
async def announce(ctx, announcement):
  await ctx.send(f"""Hello""")




TOKEN = os.environ.get("DISCORD_BOT_SECRET")
client.run(TOKEN)

【问题讨论】:

    标签: python discord.py discord.py-rewrite


    【解决方案1】:

    我将您的代码更改如下,但请确保 discort.py 与此代码位于同一文件夹中。如果您遇到错误,请在您的条目中分享错误/警告,这将有所帮助。

    from discord import *
    from discord.ext import commands
    import os
    import time
    import random
    import weather
    from weather import Weather, Unit
    
    prefix = '$'
    client = commands.Bot(command_prefix = prefix)
    clattr = Client()
    
    
    @client.event
    async def on_ready():
            await client.change_presence(status = Status.online, activity = Game('have a nice day!'))
            print("Bot is now online!")
    
    
    @client.event
    async def on_message(message):
      if message.content.startswith("$hello"):
        await message.channel.send(f"""Hi""")
        await message.channel.send(f"""Hi""")
        await message.channel.send(f"""Hi""")
        await message.channel.send(f"""Hi""")
        await message.channel.send(f"""Hi""")
        await message.channel.send(f"""Hi""")
        await message.channel.send(time())
      if message.content.startswith("$dm"): 
        user = client.get_user(393921960817721355)
        dm_channel = user.dm_channel
        if (dm_channel==None):
          dm_channel = await user.create_dm()
        await dm_channel.send(f"""Hello!""")
      if message.content.startswith("$servers"):
        await message.channel.send(clattr.guilds)
      if message.content.startswith("$help"):
        await message.channel.send(f"""This is the vanceBot. Made by vance and OYOHO#5964 (xray#4398 helped as well).\nThese are the current commands:\n$hello, which says "Hi" a few times \n$dm, which DMs a person\n$servers, which displays the number of servers that the bot is on\n$leave, to get the bot to leave\n$help, this command.\nThe prefix is $.""")
      if message.content.startswith("$pinghelp"):
        await message.channel.send(f"""||@everyone|| This is the vanceBot. Made by vance and OYOHO#5964 (xray#4398 helped as well).\nThese are the current commands:\n$hello, which says "Hi" a few times \n$dm, which DMs a person\n$servers, which displays the number of servers that the bot is on\n$leave, to get the bot to leave\n$help, this command.\nThe prefix is $.""")
    
    
    @client.command()
    async def announce(ctx, announcement):
      await ctx.send(f"""Hello""")
    
    
    
    
    TOKEN = os.environ.get("DISCORD_BOT_SECRET")
    client.run(TOKEN)
    

    【讨论】:

    • 好的,我试过了,它给出:回溯(最近一次调用最后一次):文件“main.py”,第 4 行,在 中来自 discord import * ModuleNotFoundError: No module named ' discord' 是的,我已经安装了 discord.py。
    • 你的 Python 版本是多少?你能打开 Python IDE 并运行 import discord print discord.__version__ 如果不正确安装它会显示版本。
    • discord 1.0.1 discord.py 1.2.4 Python 3.7 64bit
    猜你喜欢
    • 2020-12-26
    • 2021-10-23
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    相关资源
    最近更新 更多