File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 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' )
You can’t perform that action at this time.
0 commit comments