首页 > 解决方案 > Discord 模块从未使用过?

问题描述

我在这里比较困惑,在尝试研究答案时,我似乎没有找到任何对我有意义的东西。我创建了一个带有 5 个 cog 的 discord 机器人,在每个 cogs 中,我import discord, os, and from discord.ext import commands在其他各种 cogsimport中还有其他模块,例如视random情况而定,但这是三个常见的模块。

问题是在每个模块中,import discord都是灰色的(PyCharm IDE),表明从未使用过。尽管如此,我的机器人运行良好。我似乎无法使用wait_for()命令之类的东西,我想是因为它在discord模块中?我没有正确设置使用它吗?

我将发布初始启动模块和另一个模块的小片段,而不是列表模块。如果您需要更多信息,请告诉我。

初始启动:

import discord
import os
from discord.ext import commands

token = open("token.txt", "r").read()

client = commands.Bot(command_prefix = '!')

@client.command()
async def load(ctx, extension):
    client.load_extension("cogs." + extension)

@client.command()
async def unload(ctx, extension):
    client.unload_extension("cogs." + extension)

for filename in os.listdir("./cogs"):
    if filename.endswith('.py'):
        client.load_extension("cogs." + filename[:-3])

client.run(token)

另一个模块:

import discord
from discord.ext import commands
import os
import json
from pathlib import Path

class Sheet(commands.Cog):

    def __init__(self, client):
        self.client = client


    @commands.command()
    @commands.dm_only()
    async def viewchar(self, ctx):

         #Snipped code here to make it shorter.
         pass

    @viewchar.error
    async def stats_error(self, ctx, error):
        if isinstance(error, commands.PrivateMessageOnly):
            await ctx.send("You're an idiot, now everyone knows. Why would you want to display your character sheet "
                           "in a public room? PM me with the command.")
        else:
            raise error

def setup(client):
    client.add_cog(Sheet(client))

标签: python-3.7discord.py-rewrite

解决方案


这只是意味着您的代码不会在discord任何地方直接引用该模块。您正在通过commands模块获取所有内容。

您可以在import discord不破坏任何内容的情况下从代码中删除 ,因为依赖它的代码仍将在幕后导入和使用它。


推荐阅读