python-3.7 - 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))
解决方案
这只是意味着您的代码不会在discord
任何地方直接引用该模块。您正在通过commands
模块获取所有内容。
您可以在import discord
不破坏任何内容的情况下从代码中删除 ,因为依赖它的代码仍将在幕后导入和使用它。
推荐阅读
- r - 负数比较错误
- python - 如何通过将日期与该数据框中的日期时间对象进行比较来获取数据框中的索引值?
- scala - 如何将响应正文字段传递给其他请求的正文(加特林)
- outlook-web-addins - 调用 saveAsync 更改 ChangeKey
- java - 正则表达式或字符串操作从字符串派生自动模块名称
- laravel-5 - 新的 Laravel 应用程序页面加载缓慢
- scala - Scala/Spark:如何检查数据框是否包含特定的列列表?
- javascript - 修改没有突变的数组
- node.js - 在 Heroku 应用程序中的 Safari 上阻止 socket.io CORS 请求
- bash - 使用 UNIX less 命令更改正在显示的文件的颜色设置