問題描述
我為 Discord 編寫了一個相當(dāng)大的機器人.它有超過 1000 行代碼.當(dāng)我在 Youtube 和這里??研究如何做到這一點時,似乎沒有任何效果.我想知道是否有人可以解釋如何正確使用齒輪,可能還有照片示例.我可以展示我必須使用的代碼來幫助您理解我想要什么.
I have quite a large bot for Discord written up. It has over 1000 lines of code. When I researched how to do it on Youtube and here, nothing seems to be working. I was wondering if someone could explain how to use a cog properly, possibly with photo examples. I can show what code I have to help you understand what I want.
舉個例子,我想將我所有的 mod 命令放在一個單獨的文件中,這樣它就更干凈、更有條理了.那么,我該怎么做呢?這是我的代碼示例:
An example would be that I want to have all of my mod commands in a separate file, just so its cleaner and more organized. so, how do I go about doing this? Here is an example of my code:
Mod 命令我想使用 cog 移動到單獨的文件
Mod Commands I want to move to a separate file using a cog
我目前擁有的進口商品
前綴和目錄
調(diào)用令牌 ID - 令牌 ID 在上面,圖中未顯示:
Calling token ID - token id is above, not shown in photo:
我不確定如何完全啟動一個 cog,還要導(dǎo)入什么,如何調(diào)用文件.我很了解 Java,但我正在嘗試使用 Discord 來提高我的 Python 技能.
I am unsure how to start a cog completely, what else to import, how to call the file. I know Java well, but I am trying to work on my python skills with Discord.
推薦答案
注意:
以下內(nèi)容是為較舊的 0.16 版本編寫的,該版本沒有良好的 cogs 文檔.新的 1.0 版本有很好的文檔,并且完全改變了 cogs 的結(jié)構(gòu).如果您使用的是現(xiàn)代版本的 discord.py,你應(yīng)該查閱官方文檔.
每個 cog 都有兩部分:一個類和一個 setup
函數(shù).幾乎所有 setup
函數(shù)看起來都一樣:
Every cog has two parts: a class and a setup
function. Almost all setup
functions look the same:
def setup(bot):
bot.add_cog(Cog(bot))
其中 Cog
是 cog 類.
where Cog
is the cog class.
cog 類包含我們所有的命令和事件作為方法.
The cog class contains all of our commands and events as methods.
您需要進行四個主要的轉(zhuǎn)換才能將您的機器人變成一個齒輪:
There are four main transformations that you need to do to change your bot to a cog:
將
bot.command
替換為commands.command
(commands
是from discord.ext import commands
)
更改函數(shù)的簽名以在開頭包含 self
,因為您的所有命令和事件現(xiàn)在都是 cog 類的方法
Change the signatures of your functions to include self
at the beginning, as all of your commands and events are now methods of the cog class
將所有對 bot
的引用改為引用 self.bot
Change all references to bot
to refer to self.bot
instead
移除所有 bot.event
裝飾器.您的 cog 中的事件偵聽器僅在名稱上注冊
Remove all bot.event
decorators. Event listeners from your cog are registered on name alone
還有一些陷阱:
從 cog 中的任何
on_message
事件中刪除await bot.process_commands(message)
.對于任何消息,這應(yīng)該只等待一次.默認(rèn)的on_message
已經(jīng)為您完成了這項工作.
Remove
await bot.process_commands(message)
from anyon_message
events in your cog. For any message this should only be awaited once. The defaulton_message
does already does this for you.
通過 cog 注冊事件不會從您的主文件或其他 cog 中刪除與該事件相關(guān)的其他回調(diào).這意味著您的機器人可以多次響應(yīng) on_member_join
事件,例如,如果您在多個位置定義了該事件的行為.
Registering an event through a cog does not remove other callbacks related to that event, from your main file or other cogs. That means that your bot could respond to a on_member_join
event multiple times for example, if you have behaviour for that event defined in multiple places.
示例
假設(shè)您在目錄 src
中有以下 discord.py bot,bot.py
:
Example
Let's say you have the following discord.py bot, bot.py
in the directory src
:
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
@commands.has_role("Mod")
async def acommand(ctx, argument):
await bot.say("Stuff")
@bot.event
async def on_message(message):
print(message.content)
await bot.process_commands(message)
bot.run("token")
然后您將該功能分解為一個 cog src/cogs/maincog.py
You then factor that functionality out into a cog src/cogs/maincog.py
from discord.ext import commands
class MainCog:
def __init__(self, bot):
self.bot = bot
@commands.command(pass_context=True)
@commands.has_role("Mod")
async def acommand(self, ctx, argument):
await self.bot.say("Stuff")
async def on_message(self, message):
print(message.content)
def setup(bot):
bot.add_cog(MainCog(bot))
你的 bot.py
文件看起來像
And your bot.py
file would look like
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
bot.load_extension("cogs.maincog")
bot.run("token")
請注意,要在 cogs/maincog.py
加載擴展,我們使用 load_extension("cogs.maincog")
.
Note that to load the extension at cogs/maincog.py
, we use load_extension("cogs.maincog")
.
Cogs 還允許你定義一些特殊的方法.其中大部分僅在 discord.py-rewrite 并記錄在 here.
Cogs also allow you to define some special methods. Most of these are available only in discord.py-rewrite and are documented here.
__global_check
,以前稱為__check
,在每個命令之前運行,并且必須返回True
才能繼續(xù)執(zhí)行該命令.
__global_check
, formerly__check
, runs before every command and must returnTrue
for that command to proceed.
__local_check
僅在來自此 cog 的命令之前運行.
__local_check
runs only before commands from this cog.
__global_check_once
我相信這類似于 __global_check
只是它只在子命令的情況下檢查一次.我沒用過這么多.
__global_check_once
I believe that this is similar to __global_check
except that it only checks once in the case of subcommands. I haven't used this much.
__unload
您可以通過卸載擴展程序,然后重新加載它來實時刷新您的機器人,這樣您就可以在不讓您的機器人離線的情況下更新您的 cogs.當(dāng)您卸載擴展程序或當(dāng)您的機器人停止運行時調(diào)用此方法,以防您需要進行清理.
__unload
You can live refresh your bot by unloading the extension, then reloading it, allowing you to update your cogs without taking your bot offline. This method is called when you unload the extension, or when your bot stop running, in case you need to do cleanup.
__before_invoke
和 __after_invoke
分別在來自該 cog 的每個命令之前和之后運行.
__before_invoke
and __after_invoke
are run before and after every command from this cog, respectively.
__error
是來自此 cog 的命令的錯誤處理程序.
__error
is an error handler for commands from this cog.
這篇關(guān)于如何在 discord.py 中使用 cogs?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!