我正在尝试制作Tkinter GUI,单击GUI按钮,discord bot可以发送消息。我设法使机器人和GUI使用线程运行。但是,我不知道如何在按下按钮时使机器人发送消息。感谢您的帮助。
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from tkinter import Tk, Label, Button
import threading
### Gui
class gui:
def __init__(self, master):
self.master = master
self.button = Button(self.master, text="testButton", command=self.buttonFunc)
self.button.pack()
def buttonFunc(self):
pass
#Function of the button to say something in discord
### Discord bot
class yeeter():
global client #Added global, not sure how to include self with the discord clients at @client.event
global Client
Client = discord.Client()
client = commands.Bot(command_prefix = "")
@client.event
async def on_ready():
print("Ready")
@client.event
async def on_message(msg):
if msg.content == "-_-":
await client.send_message(msg.channel, ":expressionless:")
if msg.content.upper().startswith("PING"):
userID = str(msg.author.id)
await client.send_message(msg.channel, "<@" + userID + "> Pong! :ping_pong:")
if msg.author.id == "367323415901896700":
chance = random.randint(0,100)
if chance > 50:
userID = str(msg.author.id)
await client.add_reaction(msg, "\U0001F36A")
def yeeterCall():
y = yeeter()
client.run("token")
def guiCaller():
root = Tk()
root.geometry("300x100")
discordGui = gui(root)
root.mainloop()
try:
t = threading.Thread(target = yeeterCall)
t.start()
y = threading.Thread(target = guiCaller)
y.start()
except:
print("No new thread")
由于我是多处理新手,请对我的线程和异步代码发表评论。