【问题标题】:Pygame with discord botPygame 与不和谐机器人
【发布时间】:2017-08-12 05:48:18
【问题描述】:
import discord
import random
import pygame
import time

client = discord.Client()
white = (255,255,255)
clock = pygame.time.Clock()
green = (0,255,0)
red =(255,0,0)
black = (0,0,0)

global song
song = 0


@client.event
async def on_message(message):

        # we do not want the bot to reply to itself
    if message.author == client.user:
         return
     if message.content.startswith(''):
        while True:
            if song == 1:

                await client.send_message(message.channel, ';;play https://www.youtube.com/watch?v=cUbFzEMQ2Fs')

            elif song == 2:
                await  client.send_message(message.channel, ';;play https://www.youtube.com/watch?v=YlomIQF2zbI')
            else:
                await client.send_message(message.channel, "HI")
                pygame.quit()






def interface():



    pygame.init()
    gameDisplay = pygame.display.set_mode((500, 500))

    def button(x, y, w, h, ac, ic, songs):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if x + w > mouse[0] > x and y + h > mouse[1] > y:
            pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
            if click[0] == 1 and songs != 0:

        else:
            pygame.draw.rect(gameDisplay, ic, (x, y, w, h))

    while True:
        event = pygame.event.get()
        gameDisplay.fill(white)
        button(50, 50, 50, 50, red, green, 1)
        button(50, 50, 50, 50, red, green, 2)

        pygame.display.update()
        clock.tick(60)











@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    interface()
    '''channel = client.get_channel('id')
    await client.join_voice_channel(channel)
    print('Bot should joined the Channel')'''


client.run('token')

由于我有点卡住,有没有人对如何做有任何建议。 当我单击 pygame 显示界面上的按钮时,我希望机器人说些什么。谢谢你能给我的任何帮助,

【问题讨论】:

  • 您可以进一步查看threading。我会推荐你​​使用 tkinter,因为它是一个内置的 GUI,而且按钮非常方便:)
  • 对不起,我以前从未使用过线程
  • 嗯...为了做到这一点,你需要使用它,因为有两个事件循环。另一种方法是将两个循环结合起来,这会更令人困惑,我不会推荐它,请将您的标题编辑为未来读者可能会搜索的内容。类似于“如何使用不和谐机器人的 GUI”
  • 线程使用起来难不难
  • 不,对于初学者来说很简单,然后越高级就越复杂

标签: python discord


【解决方案1】:

虽然这不完全是您的代码,但它为您提供了使用线程同时运行它们的粗略想法。我通过示例解释了所有内容。按下方块时,它将向选定的频道发送消息。

import discord, random, pygame, time, asyncio
import random # just for fun
from threading import Thread

### better to set them as a global variable
client = discord.Client()

pygame.init() # put these in the beginning
gameDisplay = pygame.display.set_mode((500, 500))
white = (255,255,255)
clock = pygame.time.Clock()
green = (0,255,0)
red   = (255,0,0)
black = (0,0,0)
###
#client.send_message()
@client.event
async def on_message(message):
    # do what you want to do...
    pass

async def send_message(msg):
    await client.send_message(client.get_channel('197481285852069898'), msg) # send a message, you can use channel id or any channel instance

def run_gui(): # for running the gui
    gameDisplay.fill(white) # initialize the screen white
    current_color = red 
    my_rect = pygame.draw.rect(gameDisplay, current_color, (50,50,50,50)) # draw a rect and save the rect
    while 1: # pygame mainloop
        pygame.display.update() # update the screen
        for event in pygame.event.get(): # proper way of retrieving events
            if event.type == pygame.MOUSEBUTTONDOWN: # check if the event is right clicked on mouse 
                mouse = pygame.mouse.get_pos()
                if my_rect.collidepoint(mouse): # see if it pressed the rect
                    # do stuff if the button is pressed...
                    current_color = red
                    # send a random message maybe?
                    choosen = random.choice(['hello','hi','I am a bot','wassup','I luv u <3'])
                    asyncio.ensure_future(send_message( msg=choosen)) # since discord.py uses asyncio

                else:
                    # do stuff if it's not pressed...
                    current_color = green
                # refill the screen white
                gameDisplay.fill(white)
                # redraw the rect
                my_rect = pygame.draw.rect(gameDisplay, current_color, (50,50,50,50)) 

        clock.tick(60) # 60 fps

def run_bot(): # for running the bot
    client.run('token') # run the bot

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

Thread(target=run_bot).start() # start thread the run the bot
run_gui() # run the gui

【讨论】:

    猜你喜欢
    • 2021-03-25
    • 2018-03-20
    • 2020-07-03
    • 2020-09-23
    • 2021-10-30
    • 2021-08-05
    • 2021-05-27
    • 2021-07-15
    • 2022-01-07
    相关资源
    最近更新 更多