永远不会触发on_ready和on_message事件吗?
我的想法是一个类,用于处理所有发送到不和谐服务器的消息.这样以后我就可以将其与其他IRC类型的通道一起使用来同步它们了.
My idea is a class that handles all incoming and outgoing messages to a discord server. So that later on I can use this in tandem with different IRC type channel to synchronize them.
但是首先我想开发此类,但是我无法使其响应消息.我打算使用Web挂钩,这就是为什么我要使用重写库.
But first I wanted to develop this class, however I cannot make it to respond to a message. I intend to use web hooks, which is why I'm using the rewrite library.
这是我的类的外观和使用方式,但是我无法使它看到不一致的消息,而仅在响应该消息时键入 hello
.
This is how my class looks and is used, but I cannot make it see a message on discord and just type hello
in responds to that message.
class DiscordChat:
def __init__(self):
self.client = discord.Client()
self.WEBHOOK_ID = aNumber
self.WEBHOOK_TOKEN = "AWebHookToken"
self.DISCORD_BOT_TOKEN = "ABotToken"
self.__webhook = Webhook.partial(self.WEBHOOK_ID, self.WEBHOOK_TOKEN,adapter=RequestsWebhookAdapter())
def get_client(self):
return self.__client
def set_client(self,value):
self.__client = value
def send(self, message,username):
self.__webhook.send("Hello World from DiscordChat: " + message, username=username)
async def on_ready(self):
print('Logged in as')
print(self.client.user.name)
print(self.client.user.id)
print('------')
async def on_message(self,message):
print(message.content)
await message.channel.send('Hello! ' + message.author.name )
def run(self):
self.client.run(self.DISCORD_BOT_TOKEN)
client = property(get_client,set_client)
class TDCRBot:
def __init__(self):
print("initializin main program")
def run(self):
print("Running program!")
self.main()
def main(self):
print("hello from Main program!")
objDiscordChat = DiscordChat()
objDiscordChat.run()
objDiscordChat.send("Test Message for Discord Streaming text channel","TestUser Sil3ntDragon")
if __name__ == "__main__":
app = TDCRBot()
app.run()
我知道我在这里做错了,我只是无法弄清楚是什么.
I know I'm doing something wrong here I just cannot figure out what.
例如,我已经在许多示例中看到了 @ client.event
的用法,但是当我尝试使用它时,我只是得到一个错误,指出 client
不是定义.我预感到可能是问题所在,但这只会给我带来更大的问题,我应该如何定义 client
?
For instance I've seen the use of @client.event
on many examples but when I try to use it I just get an error saying that client
is not defined. I got a hunch that might be the problem but that would only leave me with a even bigger question how am I supposed to get client
defined?
由于 client.event
是装饰器,并且您知道
Since client.event
is a decorator and you know that
@dec
def func():
...
等效于:
func = dec(func)
并且您的客户端被定义为实例变量,因此您可以在 __ init __
中注册事件.这是工作代码的精简版(如果需要,您可以添加其他所有内容):
And that your client is defined as an instance variable, therefore you can register the events in __init__
. Here's a stripped down version of the working code (you can add everything else if you wish):
import discord
class DiscordChat:
def __init__(self):
self.client = discord.Client()
self.on_ready = self.client.event(self.on_ready)
self.on_message = self.client.event(self.on_message)
async def on_ready(self):
print('Logged in as')
print(self.client.user.name)
print(self.client.user.id)
print('------')
async def on_message(self, message):
print(message.content)
await message.channel.send('Hello! ' + message.author.name)
def run(self):
self.client.run("Token")
class TDCRBot:
def __init__(self):
print("initializin main program")
def run(self):
print("Running program!")
self.main()
def main(self):
print("hello from Main program!")
objDiscordChat = DiscordChat()
objDiscordChat.run()
if __name__ == "__main__":
app = TDCRBot()
app.run()