在不和谐聊天中打印4x4表情网格的功能

时间:2019-02-11 17:46:13

标签: python python-3.x discord.py

这里有新的python爱好者。

我当前正在制作一个不和谐的bot,当特定用户输入聊天内容时,它会以4x4网格响应16张图片:

XXXX

XXXX

XXXX

XXXX

这是我当前的代码:

import discord
from secrets import TOKEN
from emotes import *

# user IDs
bonvang_ID = "417010736988946444"
djursing_ID = "124806046568022016"
jonathan_ID = "151788707760832512"
alex_ID = "151745760935936001"
snuffles_ID = "221360712222507009"

# bot = commands.Bot(command_prefix='!')
client = discord.Client()

# prints message in terminal when bot is ready
@client.event
async def on_ready():
    print(f"Logged in as {client.user}")


# @client.event
# async def on_member_join(member):
#     await message.channel.send(f"""Welcome to the server {member}!\n
#         I'm just a simple bot made by Mr.Nofox.\n
#         If you want to know more about the commands, ask him or try your luck with the others! :)""")


def member_amount():
    # create a variable that contains all the servers
    active_servers = client.servers
    # create a variable to store amount of members per server
    sum = 0
    # loop through the servers, get all members and add them up, this includes bots
    for s in active_servers:
        sum += len(s.members)
    return sum


def picture_grid():
    return (f"{pic01} {pic02} {pic03} {pic04}\n{pic05} {pic06} {pic07} {pic08}\n{pic09} {pic10} {pic11} {pic12}\n{pic13} {pic14} {pic15} {pic16}\n)


@client.event
async def on_message(message):
    # print(f"{message.channel}: {message.author}: \"{message.content}\"")
    guild = client.get_all_members()

    if "!count()" == message.content:
        await client.send_message(message.channel, f"```py\nTotal members: {member_amount()}```")

    elif "!report()" == message.content:
        online = 0
        idle = 0
        offline = 0

        for member in guild:
            if str(member.status) == "online":
                online += 1
            if str(member.status) == "offline":
                offline += 1
            if str(member.status) == "idle" or str(member.status) == "dnd":
                idle += 1

        await client.send_message(message.channel, f"```Online: {online}\nIdle/busy.dnd: {idle}\nOffline: {offline}```")


    #TODO: Make a neat function for the pictures in message that responds with 4x4 image in chat when a given user types
    if (message.author.id == alex_ID or message.author.id == snuffles_ID):
        await client.send_message(message.channel, "DET ER MIN SØN!")
        await client.send_message(message.channel, picture_grid())


# add bot-token found on website
client.run(TOKEN)

所有表情都在我导入的名为“ emotes.py”的文件中。 每个表情都是一个字符串代码,例如:
pic01 = '<:1_:543049824644235265>'

我用来使它工作于atm的线。是:

await client.send_message(message.channel, f"{pic01} {pic02} {pic03} {pic04}\n{pic05} {pic06} {pic07} {pic08}\n{pic09} {pic10} {pic11} {pic12}\n{pic13} {pic14} {pic15} {pic16}\n")

这是糟糕的编码实践,因此我想制作一个返回的函数:

f"{pic01} {pic02} {pic03} {pic04}\n{pic05} {pic06} {pic07} {pic08}\n{pic09} {pic10} {pic11} {pic12}\n{pic13} {pic14} {pic15} {pic16}\n"

我最初的想法是:

def picture_grid():  
    return (f"{pic01} {pic02} {pic03} {pic04}\n{pic05} {pic06} {pic07} {pic08}\n{pic09} {pic10} {pic11} {pic12}\n{pic13} {pic14} {pic15} {pic16}\n")

这可以解决问题,并将第一个代码转换为:

await client.send_message(message.channel, picture_grid())

但是只是将错误的代码移到另一个地方。.我目前对应该是一个简单的任务感到困惑。

有什么想法吗?

0 个答案:

没有答案