Skip to content

Commit

Permalink
Added discord bot example (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
r-priyam authored Apr 12, 2021
1 parent 78879c5 commit 39fda3a
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/discord_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from io import BytesIO

import discord
from PIL import Image
from discord.ext import commands
from quickchart import QuickChart

description = '''An example bot to showcase the use of QuickChart with discord.py module.'''

intents = discord.Intents.default()

bot = commands.Bot(command_prefix='!', description=description, intents=intents)


@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')


@bot.command()
async def graph(ctx):
qc = QuickChart()
qc.width = 600
qc.height = 300
qc.device_pixel_ratio = 2.0
qc.config = {
"type": "bar",
"data": {
"labels": ["Hello world", "Test"],
"datasets": [{
"label": "Foo",
"data": [1, 2]
}]
}
}
with Image.open(BytesIO(qc.get_bytes())) as chat_sample:
output_buffer = BytesIO() # By using BytesIO we don't have to save the file in our system.
chat_sample.save(output_buffer, "png")
output_buffer.seek(0)
await ctx.send(file=discord.File(fp=output_buffer, filename="chat_sample.png")) # Change the file name accordingly.


@graph.before_invoke
async def before_test_invoke(ctx):
await ctx.trigger_typing() # Take time to render and send graph so triggering typing to reflect bot action.

bot.run('token')

0 comments on commit 39fda3a

Please sign in to comment.