Python on_就绪且on_消息事件从未触发?
我的想法是一个类,它处理所有传入和传出到discord服务器的消息。因此,稍后我可以将其与不同的IRC类型通道一起使用,以同步它们 但是首先我想开发这个类,但是我不能让它响应消息。我打算使用web钩子,这就是我使用重写库的原因 这就是我的类的外观和使用方式,但我不能让它看到关于discord的消息,而只是在响应该消息时键入Python on_就绪且on_消息事件从未触发?,python,discord,discord.py,discord.py-rewrite,Python,Discord,Discord.py,Discord.py Rewrite,我的想法是一个类,它处理所有传入和传出到discord服务器的消息。因此,稍后我可以将其与不同的IRC类型通道一起使用,以同步它们 但是首先我想开发这个类,但是我不能让它响应消息。我打算使用web钩子,这就是我使用重写库的原因 这就是我的类的外观和使用方式,但我不能让它看到关于discord的消息,而只是在响应该消息时键入hello class DiscordChat: def __init__(self): self.client = discord.Client()
hello
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()
我知道我做错了什么,我就是不知道是什么
例如,我在许多示例中看到了
@client.event
的用法,但当我尝试使用它时,我只得到一个错误,即客户端
未定义。我有一种预感,这可能是问题所在,但这只会给我留下一个更大的问题:我应该如何定义客户机 因为client.event
是一个装饰师,你知道的
@dec
def func():
...
相当于:
func = dec(func)
并且您的客户机被定义为实例变量,因此您可以在\uuuu init\uuuu
中注册事件。下面是一个精简版的工作代码(如果愿意,您可以添加其他所有内容):
我觉得有点尴尬。。我用其他语言做过这种事件处理,为什么我在这里没有想到…:/但非常感谢你快速、有启发性、详尽而又简单的回答。
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()