Skip to content

Commit 39fda3a

Browse files
authored
Added discord bot example (#6)
1 parent 78879c5 commit 39fda3a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

examples/discord_bot.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from io import BytesIO
2+
3+
import discord
4+
from PIL import Image
5+
from discord.ext import commands
6+
from quickchart import QuickChart
7+
8+
description = '''An example bot to showcase the use of QuickChart with discord.py module.'''
9+
10+
intents = discord.Intents.default()
11+
12+
bot = commands.Bot(command_prefix='!', description=description, intents=intents)
13+
14+
15+
@bot.event
16+
async def on_ready():
17+
print(f'Logged in as {bot.user.name}')
18+
19+
20+
@bot.command()
21+
async def graph(ctx):
22+
qc = QuickChart()
23+
qc.width = 600
24+
qc.height = 300
25+
qc.device_pixel_ratio = 2.0
26+
qc.config = {
27+
"type": "bar",
28+
"data": {
29+
"labels": ["Hello world", "Test"],
30+
"datasets": [{
31+
"label": "Foo",
32+
"data": [1, 2]
33+
}]
34+
}
35+
}
36+
with Image.open(BytesIO(qc.get_bytes())) as chat_sample:
37+
output_buffer = BytesIO() # By using BytesIO we don't have to save the file in our system.
38+
chat_sample.save(output_buffer, "png")
39+
output_buffer.seek(0)
40+
await ctx.send(file=discord.File(fp=output_buffer, filename="chat_sample.png")) # Change the file name accordingly.
41+
42+
43+
@graph.before_invoke
44+
async def before_test_invoke(ctx):
45+
await ctx.trigger_typing() # Take time to render and send graph so triggering typing to reflect bot action.
46+
47+
bot.run('token')

0 commit comments

Comments
 (0)